如何查看列表中是否包含元素

时间:2018-03-30 09:59:51

标签: r data.table

我有以下列表

catlist <- list(c("< 30 days","1-3 months","4-6 months"),c("7-12 months"),c("1-3 years"),
                  c("4-5 years","5+ years","never"))

我给出了名字

names(catlist) <- 1:length(catlist)

更多我有一个data.table

library(data.table)
tmp <- data.table(variable = c("never","1-3 years"))

我想要做的是在new_variable中创建一个新变量(tmp),其值为catlist的值,其中值为 variable落入

所以最后我想结束这个

> tmp
    variable new_variable
1:     never            4
2: 1-3 years            3

我尝试创建一个函数,但它不起作用

trans_dummy_multiple <- function(dt, var, catlist){

  dt <- tmp # for testing
  var <- "variable" # for testing
  catlist <- list(c("< 30 days","1-3 months","4-6 months"),c("7-12 months"),c("1-3 years"),
                  c("4-5 years","5+ years","never")) # for testing
  names(catlist) <- 1:length(catlist)
  dt[,new_variable:=lapply(catlist,function(x){if(x%in%get(var)){names(x)}})]


}

4 个答案:

答案 0 :(得分:3)

尝试:

scl<-setDT(stack(catlist))
scl[tmp,on=c(values="variable")]
#      values ind
#1:     never   4
#2: 1-3 years   3

答案 1 :(得分:1)

您可以使用grep

使用解决方案
tmp <- data.frame(variable = c("never","1-3 years"), stringsAsFactors = F)
df <- transform(tmp, new_variable = sapply(df$variable, function(item) grep(item, catlist)))
df

这会产生

   variable new_variable
1     never            4
2 1-3 years            3

答案 2 :(得分:1)

以下是melt

的一个选项
setDT(melt(catlist))[tmp, on = .(value = variable)]
#       value L1
#1      never  4
#2: 1-3 years  3

答案 3 :(得分:1)

我能够使用utils::stack()函数(获得主意here)和dplyr(我没有使用data.table的经验)来执行此操作。将其添加到您的代码中:

require(dplyr)

catlist2 <- catlist %>%
  stack()

tmp <- tmp %>%
  left_join(y = catlist2, by = c("variable" = "values"))

#   variable ind
# 1     never   4
# 2 1-3 years   3