问题描述
简单的逻辑替换问题;我有一个数据框,如:
Simple logical replace problem; I have a dataframe like:
mydf <- expand.grid(var1 = c('type1', 'type2'), var2 = c(7, 6, "NA"), var3 = 9)
我想除非var2是NA,否则用var2中的值替换var3中的值。因此,生成的新var3应为7,7,6,6,NA,NA。在尝试做到这一点时,我注意到
I would like to replace the values in var3 with the values in var2 unless var2 is NA. So the resulting new var3 should be 7,7,6,6,NA,NA. In trying to get at this, I notice that
mydf$var3[mydf$var2 == 7] <- 5
正确地将mydf的第1行和第2行标识为需要替换,只剩下最后四行,所以我得到var3 = 5,5,9,9,9,9。但是,如果我尝试
correctly identifies rows 1 and 2 of mydf as needing replacement, and leaves the last four rows alone, so I get var3 = 5,5,9,9,9,9. However if I try
mydf$var3[!is.na(mydf$var2)] <- 5
我得到var3 = 5,5,5,5,5,5。那么为什么不跳过最后两行,其中var2是NA?下一个问题是不知道如何将替换值设为var2而不是常量。
I get var3 = 5,5,5,5,5,5. So why didn't it skip the last two rows, where var2 was NA? Next problem is that don't know how to get the replacement values to be var2 instead of a constant. When I try
mydf$var3[!is.na(mydf$var2)] <- mydf$var2
我得到var3 = 1,1,2,2,3,3。我一点都不明白。
I get var3 = 1,1,2,2,3,3. Which I do not understand at all.
推荐答案
与注释中一样,字符串 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
.
这篇关于替换另一列中以is.na为条件的行值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!