How to enter the column mean in all NA values

1

Hello.

How to enter the mean in all NA values. I have a code that I read the file, check if it has na and does not, but when I transform in number, several NA appear and if I remove, the data gets well reduced from 4000 to about 600:

df<-read.csv("autores.csv", header=T, stringsAsFactors=F, sep=";")  

table(is.na(df))  #não há NA

df_numero<-lapply(df[-1], as.numeric)  

#recria o dataframe pois lapply retorna lista  
df1<-data.frame(df_numero)  

table(is.na(df))  #há NA
    
asked by anonymous 17.12.2017 / 17:52

1 answer

1

Here is code to replace NA with the average column where they are:

for(i in 1:nrow(df)){
  for(j in 1:ncol(df)){
    if(is.na(df[i,j])){
      df[i,j] <- mean(df[,j], na.rm = T)
    } 
  }
}
    
18.12.2017 / 12:28