使用dplyr :: Select s select select s base r grepl和匿名函数
这里有一个非常相似的问题:如何基于基于基于列的列dplyr :: tibble grep
但是,我认为select_if
用select(white())
超过。
我知道我可以执行以下操作,并且可以使用:
# select all columns with three characters
mtcars %>%
select(
matches("^[a-zA-Z]{3}$")
)
但是我也可以使用匿名函数(在此处所有列值而不是名称上)选择列。
mtcars %>%
select(
where(function(x)sum(is.na(x)) == 0)
)
因此,我认为我可以使用Anonymus函数和grepl
来选择列。而且这不起作用:
mtcars %>%
select(
where(
function(x) grepl("^[a-zA-Z]{3}$", x)
)
)
我该如何完成这项工作?我的意思是我总是可以使用匹配
助手。但是我只想了解如何使用
select(where())
语句在dataframe的名称上使用,而不是列中的所有值。
更新
这起作用:
mtcars %>%
select(
which(grepl("^[a-zA-Z]{3}$", names(.)))
)
但是我不确定是否没有更好的方法;)
There is a very similar question here: How to select columns based on grep in dplyr::tibble
However I think that the select_if
was superseeded with select(where())
.
I know that I can do the following and it works:
# select all columns with three characters
mtcars %>%
select(
matches("^[a-zA-Z]{3}quot;)
)
But I can also use an anonymus function (here over all the column values and not the names) to select columns.
mtcars %>%
select(
where(function(x)sum(is.na(x)) == 0)
)
So I thought I could use an anonymus function and grepl
to select columns. And this does not work:
mtcars %>%
select(
where(
function(x) grepl("^[a-zA-Z]{3}quot;, x)
)
)
How could I make this work? I mean I could always use the matches
helper. But I would just like to understand how to use
select(where())
statement over the names of the dataframe and not over all the values in a column.
Update
This works:
mtcars %>%
select(
which(grepl("^[a-zA-Z]{3}quot;, names(.)))
)
But I am not sure if there isn't a better way;)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以只使用
grep()
与select> select()
,而没有whene()
函数。您的初始尝试无效,因为在此代码中:
x
在中,其中()
函数是变量的值,而不是变量的名称。这就是为什么如果您做过之类的操作(is.numeric)
有效的原因 - 因为它可以代替实际值。You could just use
grep()
withselect()
without thewhere()
function.Your initial attempt didn't work because in this code:
the
x
in thewhere()
function are the values of the variable and not the name of the variable. That's why it works if you did something likewhere(is.numeric)
works - because it is substituting the actual values.base
r选项:输出:
Base
R option:Output: