0

我需要检测包含特定字符序列的 df/tibble 行。

seq <- "RT @AventusSystems"是我的序列

df <- structure(list(text = c("@AventusSystems Wow, what a upgrade from help of investor", 
"RT @AventusSystems: A recent article about our investors as shown in Forbes! t.co/n8oGwiEDpu #Aventus #GlobalAdvisors #4thefans #Ti…", 
"@AventusSystems Very nice to have this project", "RT @AventusSystems: Join the #TicketRevolution with #Aventus today! #Aventus #TicketRevolution #AventCoin #4thefans t.co/OPlyCFmW4a"
), Tweet_Id = c("898359464444559360", "898359342952439809", "898359326552633345", 
"898359268226736128"), created_at = structure(c(17396, 17396, 
17396, 17396), class = "Date")), .Names = c("text", "Tweet_Id", 
"created_at"), row.names = c(NA, -4L), class = c("tbl_df", "tbl", 
"data.frame"))

select(df, contains(seq))
# A tibble: 4 x 0

sapply(df$text, grepl, seq)只返回 4 FALSE

我错了什么?什么是正确的解决方案?谢谢你的帮助

4

1 回答 1

2

首先,grepl已经对其参数进行了矢量化x,因此您不需要sapply. 你可以这样做grepl(seq, df$text)

为什么您的代码不起作用是 sapply 将参数的每个元素作为第一个参数传递给X参数中的函数FUN(因此您正在寻找搜索模式“@AventusSystems Wow,从投资者的帮助下进行了怎样的升级”等。在你的seq对象中。

最后,dplyr::select选择列,而您想使用dplyr::filter过滤行。

于 2017-08-22T14:31:20.947 回答