[英]How to use both starts_with and ends_with at the same time in one select statement?
I want select all columns starting with fy
and ending with giving
using dplyr
. 我想选择所有以
fy
开头并以使用dplyr
giving
列结束。 I tried the following code 我尝试了以下代码
df %>% select(start_with('fy') & ends_with('giving')
but it didn't work. 但它不起作用。
p/s: actually I can hack it with the following chunk p / s:实际上我可以使用以下块来破解它
df %>% select(starts_with('fy')) %>% select(ends_with('giving'))
But I still want to put all the two conditions in one place 但我仍然希望将所有这两个条件放在一个地方
You can try using matches
instead with a regular expression: 您可以尝试使用
matches
和正则表达式:
df %>% select(matches("^fy.*giving$"))
should do the job. 应该做的工作。
A dummy example using iris
: 使用
iris
的虚拟示例:
iris %>% select(matches("^Pet.*gth$")) %>% colnames
[1] "Petal.Length"
you can use this: 你可以用这个:
df %>% select(intersect(starts_with('fy') , ends_with('giving')))
added same example as @Vincent Bonhomme: 添加了与@Vincent Bonhomme相同的例子:
iris %>% select(intersect(starts_with("Pet"), ends_with("gth"))) %>% colnames
#[1] "Petal.Length"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.