目前我的df如下所示:
df <- data.frame(
player = c('Player To Have 1 Or More Shots On Target', 'Player To Have 1 Or More Shots On Target',
'Player To Have 2 Or More Shots On Target', 'Player To Have 3 Or More Shots On Target',
'Player To Have 1 Or More Shots On Target in 1st Half'))
输出:
player
1 Player To Have 1 Or More Shots On Target
2 Player To Have 1 Or More Shots On Target
3 Player To Have 2 Or More Shots On Target
4 Player To Have 3 Or More Shots On Target
5 Player To Have 1 Or More Shots On Target in 1st Half
我想使用grepl(或另一个合适的替代方案)来仅捕获目标上的1、2、3、4等镜头(忽略其他任何内容,如第5行,其中也包含"in first Half").
在上面的示例中,我希望捕获前4行(原始数据有更多行).我try 了以下有效的方法:
df2 <- dplyr::filter(df, grepl("Player To Have 1 Or More Shots On Target", player))
如何才能省略上述字符,使其包含多个数字"1"?例如,我想拍摄1、2、3、4等镜头?
我try 了一些类似的方法:
number_of_shots <- c("1","2")
df2 <- dplyr::filter(df, grepl("Player To Have", number_of_shots, "Or More Shots On Target", player))
但我得到以下错误:
Error in `dplyr::filter()`:
ℹ In argument: `grepl(...)`.
Caused by error:
! `..1` must be of size 5 or 1, not size 2.