2014-05-02 58 views
0

列表我有一個字符串的「載體」和字符串「data.frame」的「列表」如下搜索和使用中的R

lst <- list(c("key", "parking", "velvet"), c("sumatra", "cap"), c("sled", "card"), c("notice", "piece", "page")) 

df <- c("key", "sumatra", "band", "cattle", "camp", "sled", "page", "wire", "key", "card", "cap", "page") 
df <- data.frame(df, stringsAsFactors=FALSE) 

編碼我想添加列數據幀df其中代碼基於列表lst中的向量中的成員資格。期望的輸出是這樣的。

df$code <- c("G1", "G2", "", "", "", "G3", "G4", "", "G1", "G3", "G2", "G4") 

df 
     df code 
1  key G1 
2 sumatra G2 
3  band  
4 cattle  
5  camp  
6  sled G3 
7  page G4 
8  wire  
9  key G1 
10 card G3 
11  cap G2 
12 page G4 

如何在R中做到這一點?

回答

1

這裏有一種方法:

names(lst) <- paste0('G', seq_along(lst)) 
transform(df, code=with(stack(lst), ind[match(df, values)])) 
#   df code 
# 1  key G1 
# 2 sumatra G2 
# 3  band <NA> 
# 4 cattle <NA> 
# 5  camp <NA> 
# 6  sled G3 
# 7  page G4 
# 8  wire <NA> 
# 9  key G1 
# 10 card G3 
# 11  cap G2 
# 12 page G4 
0

我假設你開始像這樣的代碼:

MyCode <- c("G1", "G2","G3", "G4", "G1", "G3", "G2", "G4") 

,但你需要知道要放什麼東西行他們試試這個:

df$code<-NA 
df[df$df %in% unlist(lst),]$code<-MyCode 

unlist()部分將轉你的列表變成矢量。 %in%部分將返回df$df匹配lst中的某個行的任何行。在沒有匹配的情況下,將會有NAdf$code之下。

2
df$code <- paste0("G",cumsum(c(TRUE, diff(sequence(sapply(lst,length)))<0)))[match(df$df, unlist(lst))] 
df$code[is.na(df$code)] <- '' 
1

下面是一個使用qdapTools包的方法:

library(qdapTools) 
names(lst) <- paste0("G", 1:length(lst)) 
df$code <- df[, 1] %l% lst 
1

而且多了一個良好的措施...

lst <- list(c("key", "parking", "velvet"), c("sumatra", "cap"), 
      c("sled", "card"), c("notice", "piece", "page")) 
d <- c("key", "sumatra", "band", "cattle", "camp", 
     "sled", "page", "wire", "key", "card", "cap", "page") 
DF <- data.frame(d, stringsAsFactors=FALSE) 

> l <- rep(seq_along(lst), sapply(lst, length)) 
> m <- l[match(d, unlist(lst))] 
> DF$code <- ifelse(is.na(m), "", paste0("G", m)) 
> DF 
##   df code 
## 1  key G1 
## 2 sumatra G2 
## 3  band  
## 4 cattle  
## 5  camp  
## 6  sled G3 
## 7  page G4 
## 8  wire  
## 9  key G1 
## 10 card G3 
## 11  cap G2 
## 12 page G4