我需要根据另外两列的条件用 NA 替换数值。
这是我的可重现示例:
library(dplyr)
data1 <- read.csv(text = "
site,day,biomass,aereal,root,ei.obs
siteA,50,464.65,2020.3,307.3,0.84
siteA,NA,NA,NA,NA,NA
siteA,NA,NA,NA,NA,NA
siteA,59,1222.565,2159.5,148.3,0.93
siteA,NA,NA,NA,NA,NA
siteA,NA,NA,NA,NA,NA
siteA,66,1250.86,2046.8,159.1,0.92
siteB,50,464.65,2020.3,307.3,0.84
siteB,NA,NA,NA,NA,NA
siteB,NA,NA,NA,NA,NA
siteB,59,1222.565,2159.5,148.3,0.93
siteB,NA,NA,NA,NA,NA
siteB,NA,NA,NA,NA,NA
siteB,66,1250.86,2046.8,159.1,0.92")
data1.1 <- data1 %>%
mutate(ei.obs = if_else(site == "siteA" & day == 66, NA , ei.obs))
这是我得到的错误:
Error: Problem with `mutate()` input `ei.obs`.
x `false` must be a logical vector, not a double vector.
i Input `ei.obs` is `if_else(site == "siteA" & day == 66, NA, ei.obs)`.
或者,我试过这个:
data1.1 <- data1 %>%
mutate(ei.obs = na_if(ei.obs, site == "siteA" & day == 66))
但是数据框中没有任何变化。
预期的结果是这样的: