【发布时间】:2020-12-24 16:00:19
【问题描述】:
当我尝试在 DF 的 Age 列中添加带有 NA 的第二行并运行代码来清理它时,我得到了一个错误。我想知道如何解决它。我是 R 新手,这个例子来自 Udemy 课程。这不是一个测验问题 - 它是演示文稿的一部分。
我添加了额外的行,因为我想看看代码是否可以工作。
我确实搜索过这个论坛,但找不到我理解的答案。
#create the df cols
Country <- c("France", "Spain", "Germany", "Spain", "Germany", "France", "Spain", "France","Germany", "France")
Age <- c(44, 27, 30, 38, 40, 35, NA, 48, 50, 37)
Salary <- c(72000, 48000, 54000, 61000, NA, 58000, 52000, 79000, 830000, 67000)
Purchased <- c("No","Yes","No","No","Yes","Yes","No","Yes","No","Yes")
#create the df
empdata <- data.frame(Country, Age, Salary, Purchased)
#The logic to set the 'NA' values to the mean will not work when you add this record, WHY?
empdata[nrow(empdata) + 1, ] = c('Spain', NA , 67000.00, "Yes")
#Run this logic to fix the 'NA' Values
empdata$Age = ifelse(is.na(empdata$Age),
ave(empdata$Age, FUN = function(x) mean(x, na.rm= TRUE)),
empdata$Age)
empdata$Salary = ifelse(is.na(empdata$Salary),
ave(empdata$Salary, FUN = function(x) mean(x, na.rm = TRUE)),
empdata$Salary)
#view the data
empdata
【问题讨论】: