Reputation: 1155
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?
Upvotes: 0
Views: 31
Reputation: 145765
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
Upvotes: 1