我有以下数据框
我想用这个向量替换NA(除了它是我的类数字,但是我把它改成as.character()并且仍然有同样的错误)
L8Replace <- c("0.56","0.57","0.87",0.81")
我试过了
mydata[is.na(mydata$Freq2),] <- L8replace
但是得到
Error in as.Date.numeric(value) : 'origin' must be supplied
我理解为什么R很困惑,但我不知道如何解决它
供您使用,这是我通过dput输入数据时得到的结果:
mydata <- structure(list(Freq2 = structure(c(0.42, 0.6, 0.43, NA, NA, NA,
NA, NA), .Names = c("2015-04-16", "2015-04-21", "2015-04-26",
"", "", "", "", "")), myDate = structure(c(16541, 16546, 16551,
16627, 16643, 16659, 16675, 16691), class = "Date"), numDays = structure(c(0,
5, 10, 86, 102, 118, 134, 150), class = "difftime", units = "days")), .Names = c("Freq2",
"myDate", "numDays"), row.names = c("2015-04-16", "2015-04-21",
"2015-04-26", "7", "8", "9", "10", "11"), class = "data.frame")
答案 0 :(得分:0)
我们需要替换特定的列
mydata$Freq2[is.na(mydata$Freq2)] <- L8Replace
mydata
# Freq2 myDate numDays
#2015-04-16 0.42 2015-04-16 0 days
#2015-04-21 0.60 2015-04-21 5 days
#2015-04-26 0.43 2015-04-26 10 days
#7 0.56 2015-07-11 86 days
#8 0.57 2015-07-27 102 days
#9 0.87 2015-08-12 118 days
#10 0.81 2015-08-28 134 days
#11 0.00 2015-09-13 150 days
另外,L8Replace&#39;的length
应与“Freq2&#39;
L8Replace <- c(0.56, 0.57, 0.87,0.81, 0)