根据另一列的值将多列替换为丢失

时间:2018-12-11 15:46:43

标签: r dplyr data-manipulation missing-data

如果许多列的值大于其他列的值,我想用一种有效的方法将它们替换为NA

例如:如果height1:height5列中的所有值为NA,则将其替换为>height6

set.seed(1)

# example data
N <- 200
data <- data.frame(id = 1:N,
               height1 = rnorm(N,50:60),
               height2 = rnorm(N,60:70),
               height3 = rnorm(N,70:80),
               height4 = rnorm(N,80:90),
               height5 = rnorm(N,90:100),
               height6 = rnorm(N,60:110))

1 个答案:

答案 0 :(得分:3)

您可以使用dplyr

data %>% mutate_at(
   vars(height1:height5),
   ~ifelse(.x > data$height6, NA, .x)
   )