【发布时间】:2021-11-06 00:19:11
【问题描述】:
我有一个数据框
ID_Student Text_Message
1 John Doe Hell like I want to fxxk around
2 Peter Gynn You such an ass
3 Jolie Hope Go to hell
我有矢量
> Ban_words
[1] "fxxk" "ass" "hell"
如何将使用任何禁用词的学生的 ID 与他们使用的词一起返回? 有什么想法吗?
到目前为止我的解决方案。
数据
ID_Student <- c("John Doe", "Peter Gynn", "Jolie Hope", "Mike Tyson")
Text_Message <- c("hell I want to fxxk around", "You such an ass", "Go to hell", "I love you")
Ban_words <- c("fxxk", "ass", "hell")
Student_Message <-data.frame(ID_Student,Text_Message)
数据框应该是这样的
ID_Student Text_Message
1 John Doe hell I want to fxxk around
2 Peter Gynn You such an ass
3 Jolie Hope Go to hell
4 Mike Tyson I love you
代码
for (i in Ban_words){
Detention_List<-Student_Message %>% filter (grepl(i, Text_Message))%>%
pull(ID_Student)
print(Detention_List)
}
返回
[1] "John Doe"
[1] "Peter Gynn"
[1] "John Doe" "Jolie Hope"
因此,对于乐队单词“fxxk”,只有约翰使用它。但是对于“地狱”这个词,约翰和朱莉都使用了它。
【问题讨论】: