更新时间:2022-12-10 09:28:11
与注释中一样,字符串 NA
不是 NA
值。因此 is.na( NA)
是 FALSE
,并且所有行均已选中。只需将您定义中的 NA
替换为 NA
。
As in the comments, the string "NA"
is not an NA
value. So is.na("NA")
is FALSE
and all rows are selected. Just replace "NA"
in your definition with NA
.
mydf <- expand.grid(var1 = c('type1', 'type2'), var2 = c(7, 6, NA), var3 = 9)
mydf$var3[!is.na(mydf$var2)] <- mydf$var2[!is.na(mydf$var2)]
请注意,您不能只用 mydf $ var2
替换左侧,因为它们现在的长度不相等-在您使用之前没有任何错误,因为什么都不是 NA
。
Note that you can't just replace the left hand side with just mydf$var2
because they now have unequal lengths - before you didn't have this error since nothing was NA
.