有人知道如何使用%>%
或%<>%
来做到这一点:
cars[cars$speed < 10 & cars$dist < 95 | cars$speed > 100,] <- NA
我尝试过:
cars %>%
.[.$speed < 10 &.$dist < 95 | .$dist > 100,] %>%
set(., j=1:dim(.)[2], value = NA)
但这不是正确的方法。
答案 0 :(得分:1)
一种方法是计算要更改的行索引,然后使用replace
mutate_all
个值
library(dplyr)
cars %>%
mutate(inds = (speed < 10 & dist < 95) | speed > 100) %>%
mutate_all(funs(replace(.,inds, NA))) %>%
select(-inds)
# speed dist
#1 NA NA
#2 NA NA
#3 NA NA
#4 NA NA
#5 NA NA
#6 NA NA
#7 10 18
#8 10 26
#9 10 34
#10 11 17
#...