我有"宽幅"数据开始:
library(package = dplyr)
data <- data %>%
mutate(my_obs = select(var_1:var_12) %>%
apply(MARGIN = 1,
FUN = function(x) any(x == 1,
na.rm = TRUE)) == TRUE) %>%
filter(my_obs == 1)
整个数据集包含数千个观测值和数百个变量。我尝试过滤var_1到var_12中任何值为1的观察值。
我的尝试
Error in mutate_impl(.data, dots):
no applicable method for "select_" applied to an object of class
c("double", "numeric")
生成以下错误:
{{1}}
答案 0 :(得分:2)
我们可以使用rowSums
电话
mutate
data %>%
mutate(special_sum = rowSums(.[grep("var_\\d+", names(.))], na.rm = TRUE)) %>%
filter(special_sum >=1)
如果我们不想创建special_sum
,那么这也可以在filter
内完成
data %>%
filter(rowSums(.[grep("var_\\d+", names(.))], na.rm = TRUE)>=1)