如何解决保留选择列并根据字符串过滤其余列 数据
我有一个数据集,我想按特定字符串过滤掉,但除了过滤的列外,还保留两列。
例如。
help <- data.frame(
data = c(type,100,110,110),user1 = c("red","yes","no","yes"),user2 = c("blue",user3 = c("red",user4 = c("blue",more_data = c(5,3,6,4,3))
我希望过滤掉数据第一行中颜色为“红色”的用户,同时保留 data
和 more_data
。
例如,我的最终数据集如下所示:
data user1 user3 more_data
1 type red red 5
2 100 yes yes 3
3 100 no no 6
4 110 yes yes 3
5 110 no no 4
6 110 yes yes 3
这是某种过滤器 + grepl 命令,用于过滤蓝色的反向? filter(help,grepl(!"blue",help))
但这不起作用。
解决方法
我们可以使用 select
和 where
来检查 any
'red' 值或在 first
元素中
library(dplyr)
help %>%
select(data,where(~ 'red' %in% first(.)),more_data)
-输出
# data user1 user3 more_data
#1 type red red 5
#2 100 yes yes 3
#3 100 no no 6
#4 110 yes yes 3
#5 110 no no 4
#6 110 yes yes 3
数据
help <- structure(list(data = c("type","100","110","110"
),user1 = c("red","yes","no","yes"),user2 = c("blue",user3 = c("red",user4 = c("blue",more_data = c(5,3,6,4,3)),class = "data.frame",row.names = c(NA,-6L))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。