我有以下数据。
stringstosearch <- c("to", "and", "at", "from", "is", "of")
set.seed(199)
datatxt <- data.frame(id = c(rnorm(5)),
x = c("Contrary to popular belief, Lorem Ipsum is not simply random text.",
"A Latin professor at Hampden-Sydney College in Virginia",
"It has roots in a piece of classical Latin ",
"literature from 45 BC, making it over 2000 years old.",
"The standard chunk of Lorem Ipsum used since"))
我想要搜索列出的关键字stringtosearch
并为每个关键字创建包含结果的列。
我试过
library(stringr)
datatxt$result <- str_detect(datatxt$x, paste0(stringstosearch, collapse = '|'))
返回
> datatxt$result
[1] TRUE TRUE TRUE TRUE TRUE
然而,我正在寻找一种为每个单词创建一个布尔向量的方法stringstosearch
,即
id x to and at from is of
1 -1.9091427 Contrary to popular belief, Lorem Ipsum is not simply random text. TRUE FALSE FALSE FALSE TRUE TRUE
2 0.5551667 A Latin professor at Hampden-Sydney College in Virginia FALSE FALSE TRUE FALSE FALSE FALSE
3 -2.2163365 It has roots in a piece of classical Latin FALSE FALSE FALSE FALSE FALSE FALSE
4 0.4941455 literature from 45 BC, making it over 2000 years old. FALSE FALSE FALSE TRUE FALSE FALSE
5 -0.5805710 The standard chunk of Lorem Ipsum used since FALSE FALSE FALSE FALSE FALSE FALSE
知道如何实现这个吗?