简体   繁体   中英

Replacing NA for mean df column by using a for-loop not working and not raising any error in R

I am trying to replace NA values for mean df columns based on column data class,

I am currently using a for-loop however it does not work and it does not raise any error into R console

Here's the code I am using:

## data ##

df <- structure(list(
      name = c("Bob", "John", "Maria", "Emma", "Greg"),
      salary = c(1000.234, 2000.4322, 3000.133, 4000.765, NA),
      age = c(NA, NA, 33, 27, 54),
      tenure = c(7, 8, NA, NA, NA)),
      class = "data.frame", 
      row.names = c(NA, 
                    -5L))

## for-loop ##

for(i in 1:ncol(df)){
   if(class(df[[i]]) == "integer"){
  
 df[is.na(df[,i]), i] <- round(mean(df[,i], na.rm = TRUE), 
                                                           digits = 0)
   }
    else if(class(df[[i]]) == "numeric") {
      
      df[is.na(df[,i]), i] <- mean(df[, i], na.rm = TRUE)
    } 
      else {
             break
      }
}

Is there any other way to apply this for-loop?

for(i in 1:ncol(df)) {
  if (is.integer(df[[i]])) {
    df[is.na(df[[i]]), i] <- round(mean(df[[i]], na.rm = TRUE), digits = 0)
  }
  else if(is.numeric(df[[i]])) {
    df[is.na(df[[i]]), i] <- mean(df[[i]], na.rm = TRUE)
  }
}

df
#    name   salary age tenure
# 1   Bob 1000.234  38    7.0
# 2  John 2000.432  38    8.0
# 3 Maria 3000.133  33    7.5
# 4  Emma 4000.765  27    7.5
# 5  Greg 2500.391  54    7.5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM