gpt4 book ai didi

r - 在列中查找值返回 TRUE/FALSE

转载 作者:行者123 更新时间:2023-12-04 02:23:05 24 4
gpt4 key购买 nike

如果这是我的代码:

df<-data.frame(speaker=c("nancyball","nancyball","wigglet","wigglet"),
phrase=c("the cat is on the hat",
"the cat runs",
"the cat is under the bowl",
"a cat plays"))

prep.list<-c("on","under","in")

我想将一个新列 (df$kind) 添加到 df,其值为 TF 来自 prep.list 的单词在 df$phrase 中。

必须有一个简单的方法来做到这一点。

更好的是,我希望 df$kind 返回一些不同的东西,就像我还有:

verb.list<-c("plays","sings","sits")

我会得到:

"prep, F, prep, verb"

我试过:where() 不会将我的列强制转换为向量,并且apply()grep() 但它丢失了一个维度

最佳答案

你可以试试:

  df$kind <- grepl(paste(prep.list, collapse="|"), df$phrase)
df
# speaker phrase kind
#1 nancyball the cat is on the hat TRUE
#2 nancyball the cat runs FALSE
#3 wigglet the cat is under the bowl TRUE
#4 wigglet a cat plays FALSE


indx1 <- grepl(paste(prep.list, collapse="|"), df$phrase)
indx2 <- grepl(paste(verb.list, collapse="|"), df$phrase)

找到@Jaap 的答案后,我猜你想要:

  df$kind <- c("F", "prep", "verb")[as.numeric(factor(1*indx1+2*indx2))] #updated based on comments from @alexis_laz
df
# speaker phrase kind
#1 nancyball the cat is on the hat prep
#2 nancyball the cat runs F
#3 wigglet the cat is under the bowl prep
#4 wigglet a cat plays verb

更新

假设您有多个列表,并且不止一个列表匹配df$phrase的特定元素,一种方法是:

 new.list <- c("hat", "bowl", "howl")
nm1 <- ls(pattern=".list")
lst1 <- mget(nm1)
indx2 <- sapply(names(lst1), function(x) {x1 <- gsub("\\..*", "", x)
indx <- grepl(paste(lst1[[x]], collapse="|"), df$phrase)
c(NA, x1)[indx+1]})

df$kind <- ifelse(rowSums(is.na(indx2))==ncol(indx2), "F",
apply(indx2, 1, function(x) paste(x[!is.na(x)], collapse="_")))

df
# speaker phrase kind
#1 nancyball the cat is on the hat new_prep
#2 nancyball the cat runs F
#3 wigglet the cat is under the bowl new_prep
#4 wigglet a cat plays verb

关于r - 在列中查找值返回 TRUE/FALSE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25888024/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com