A relatively simple modification of your code should solve the issue:
for(i in 1:ncol(data)){
data[is.na(data[,i]), i] <- mean(data[,i], na.rm = TRUE)
}
More Related Contents:
- How do I replace NA values with zeros in an R dataframe?
- Replace all NA with FALSE in selected columns in R
- How to replace NA (missing values) in a data frame with neighbouring values
- Remove rows with all or some NAs (missing values) in data.frame
- Include levels of zero count in result of table()
- Insert rows for missing dates/times
- Replace NA with previous or next value, by group, using dplyr
- Replace NA in column with value in adjacent column
- Coalesce two string columns with alternating missing values to one
- Filling missing value in group
- Reading multiple files and calculating mean based on user input
- Fill missing dates by group
- Randomly insert NAs into dataframe proportionaly
- Convert NA into a factor level
- Select NA in a data.table in R
- How to change formats of multiple variables in one dataset?
- Create group names for consecutive values
- Take random sample by group
- Why is using update on a lm inside a grouped data.table losing its model data?
- Save plot with a given aspect ratio
- What is the algorithm behind R core’s `split` function?
- dplyr: lead() and lag() wrong when used with group_by()
- Imported a csv-dataset to R but the values becomes factors
- Why (or when) is Rscript (or littler) better than R CMD BATCH?
- DT[!(x == .)] and DT[x != .] treat NA in x inconsistently
- How do I tell the R interpreter how to use the proxy server?
- How to cross-paste all combinations of two vectors (each-to-each)?
- how do you order Months in ggplot
- Error in file(file, “rt”) : invalid ‘description’ argument in complete.cases program
- fast large matrix multiplication in R