合并R中列表中的字符串向量

时间:2014-04-30 12:19:29

标签: string r list merge match

我有一组字符串以及ID中的相应ID:字符串格式作为R中的向量列表

d <- list( c("SD1:LUSH", "SD44:CANCEL", "SD384:FR563", "SD32:TRUMPET"), c("SD23:SWITCH", "SD1:LUSH", "SD567:TREK"), c("SD42:CRAYON", "SD345:FOX", "SD183:WIRE"), c("SD345:HOLE", "SD340:DUST", "SD387:ROLL"), c("SD455:TOMATO", "SD39:MATURE"), c("SD12:PAINTING", "SD315:MONEY31", "SD387:SPRING"),  c("SD32:TRUMPET", "SD1:FIELD"))

[[1]]
[1] "SD1:LUSH"     "SD44:CANCEL"  "SD384:FR563"  "SD32:TRUMPET"

[[2]]
[2] "SD23:SWITCH" "SD1:LUSH"    "SD567:TREK" 

[[3]]
[3] "SD42:CRAYON" "SD345:FOX"   "SD183:WIRE" 

[[4]]
[4] "SD345:HOLE" "SD340:DUST" "SD387:ROLL"

[[5]]
[5] "SD455:TOMATO" "SD39:MATURE" 

[[6]]
[6] "SD12:PAINTING" "SD315:MONEY31"    "SD387:SPRING" 

[[7]]
[7] "SD32:TRUMPET" "SD1:FIELD" 

我想通过ID来合并矢量。需要合并具有公共ID的向量,同时保持其对应的字符串以形成新的向量。重复ID:可以在此类合并字符串中删除字符串组合。总数据包含大约2000个这样的向量。样本数据的所需输出是

out <- c("SD1:LUSH, SD1:FIELD,  SD23:SWITCH, SD32:TRUMPET, SD44:CANCEL, SD384:FR563,  SD567:TREK", "SD12:PAINTING, SD42:CRAYON, SD183:WIRE, SD340:DUST SD345:FOX, SD345:HOLE, SD387:SPRING, SD387:ROLL", "SD455:TOMATO, SD39:MATURE") 

[1] "SD1:LUSH, SD1:FIELD,  SD23:SWITCH, SD32:TRUMPET, SD44:CANCEL, SD384:FR563,  SD567:TREK"            
[2] "SD12:PAINTING, SD42:CRAYON, SD183:WIRE, SD315:MONEY31, SD340:DUST SD345:FOX, SD345:HOLE, SD387:SPRING, SD387:ROLL"
[3] "SD455:TOMATO, SD39:MATURE"

我尝试将其转换为data.frame以使用merge(),但发现它没用。是否可以首先使用字符串的ID部分搜索相交,然后使用相应矢量的并集。我尝试过使用intersect()union(),但我没有使用向量的ID部分。

我是编写R脚本的新手。

更新 正如@CarlWitthoft指出的那样,我正在努力使匹配条件更清晰地与此图像合并。 Match criteria

简而言之,我希望合并在SDxyz:___方面具有交集的向量,或者尝试获得重叠字符串向量的并集。

解决了!!

3 个答案:

答案 0 :(得分:1)

创建一个data.table Bloc,其中一列包含原始组,另一列包含分隔的ID

d <- list( c("SD1:LUSH", "SD44:CANCEL", "SD384:FR563", "SD32:TRUMPET"), c("SD23:SWITCH", "SD1:LUSH", "SD567:TREK"), c("SD42:CRAYON", "SD345:FOX", "SD183:WIRE"), c("SD345:HOLE", "SD340:DUST", "SD387:ROLL"), c("SD455:TOMATO", "SD39:MATURE"), c("SD12:PAINTING", "SD315:MONEY31", "SD387:SPRING"),  c("SD32:TRUMPET", "SD1:FIELD"))
d2 <-  lapply(d, function(x) sapply(strsplit(x, ":"), "[", 1))

d <- lapply(d, paste0, collapse=", ")
d2 <- lapply(d2, paste0, collapse=", ")

d <- as.data.frame(as.matrix(lapply(d, paste0, collapse=", ")))
d2 <- as.data.frame(as.matrix(lapply(d2, paste0, collapse=", ")))

d <- as.data.frame(cbind(d,d2))
colnames(d) <- c("sdw", "sd")
d$sd <- as.character(d$sd)
d$sdw <- as.character(d$sdw)

require(data.table)

Bloc <- data.table( d , key = "sd" )

获取所有ID以及Bloc

中的相应数据
Bloc <- Bloc[ , list( ID = unlist( strsplit( sd , "," ) ) ) , by = list(sdw, sd) ]
Bloc$ID <- gsub("^\\s+|\\s+$", "", Bloc$ID)
Bloc <- data.table( Bloc , key = "ID" )

循环以合并具有在它们之间相交的ids的向量

Bloc <- as.data.frame(Bloc)
M <- nrow(Bloc)
#create blankd data.frame
G <- data.frame(matrix(ncol=3), stringsAsFactors=FALSE)
G[,1:3] <- as.character(G[,1:3])
#G <- data.frame(sdw=character(), sd=character(), ID= character())
colnames(G) <- c("sdw", "sd", "ID")
N <- M
mch <- as.data.frame(Bloc)
#Loop to sequentially fill data.frame
for (i in 1:M) {
  # test if ID already in previous groups
  if(Bloc[i,"ID"] %in% G$ID == FALSE) { 
    # convert element to vector to check for intersect
    tm <- strsplit(x=Bloc[i, "sd"], split=", ")
    mch$t <- numeric(length=M)
  }
  for (j in 1:N){
    #if intersect exists apply code as 1 mch$t column
    ff <- strsplit(x=mch[j, "sd"], split=", ")[[1]]
    dd <- intersect (tm[[1]], ff)
    if (identical(dd, character(0))== FALSE) mch[j,"t"] = 1
  }
  submch <- subset(mch, t == 1 )
  ID <- submch$ID
  Group1 <- sort((unlist(strsplit(paste0(submch$sdw, collapse=","), ","))))
  Group1 <- unique(gsub(" ","", Group1))
  sdw <- rep(paste0(Group1, collapse=", "), nrow(submch))
  Group2 <- sort((unlist(strsplit(paste0(submch$sd, collapse=","), ","))))
  Group2 <- unique(gsub(" ","", Group2))
  sd <- rep(paste0(Group2, collapse=", "), nrow(submch))
  G1 <- cbind(sdw, sd, ID)
  G1 <- unique(G1)
  G <- rbind(G, G1)
  mch$t <- NULL
}

G <- unique(G)
G2 <- data.table(G, key="ID")
G2 <- G2[, list(sdw = paste0(sort(unique(unlist(strsplit(sdw, split=", ")))), collapse=", "), 
                sd = paste0(sort(unique(unlist(strsplit(sd, split=", ")))), collapse=", "))  , by = "ID"]
G2 <- data.table( G2, key=c("sd", "sdw"))
G2 <- unique(G2)

获取输出为data.table

Bloc <- G2[-1,]
Bloc$ID <- NULL

重复上述循环,直到不再有相交

repeat
{
  N1 <- nrow(Bloc)
  Bloc <- Bloc[ , list( ID = unlist( strsplit( sd , "," ) ) ) , by = list(sdw, sd) ]
  Bloc$ID <- gsub("^\\s+|\\s+$", "", Bloc$ID)
  Bloc <- data.table( Bloc , key = "ID" )

  Bloc <- as.data.frame(Bloc)
  M <- nrow(Bloc)
  #create blankd data.frame
  G <- data.frame(matrix(ncol=3), stringsAsFactors=FALSE)
  G[,1:3] <- as.character(G[,1:3])
  #G <- data.frame(sdw=character(), sd=character(), ID= character())
  colnames(G) <- c("sdw", "sd", "ID")
  N <- M
  mch <- as.data.frame(Bloc)
  #Loop to sequentially fill data.frame
  for (i in 1:M) {
    # test if ID already in previous groups
    if(Bloc[i,"ID"] %in% G$ID == FALSE) { 
      # convert element to vector to check for intersect
      tm <- strsplit(x=Bloc[i, "sd"], split=", ")

      mch$t <- numeric(length=M)
    }
    for (j in 1:N){
      #check if intersect exists and code accordingly
      ff <- strsplit(x=mch[j, "sd"], split=", ")[[1]]
      dd <- intersect (tm[[1]], ff)
      if (identical(dd, character(0))== FALSE) mch[j,"t"] = 1
    }
    submch <- subset(mch, t == 1 )
    ID <- submch$ID
    Group1 <- sort((unlist(strsplit(paste0(submch$sdw, collapse=","), ","))))
    Group1 <- unique(gsub(" ","", Group1))
    sdw <- rep(paste0(Group1, collapse=", "), nrow(submch))
    Group2 <- sort((unlist(strsplit(paste0(submch$sd, collapse=","), ","))))
    Group2 <- unique(gsub(" ","", Group2))
    sd <- rep(paste0(Group2, collapse=", "), nrow(submch))
    G1 <- cbind(sdw, sd, ID)
    G1 <- unique(G1)
    G <- rbind(G, G1)
    mch$t <- NULL
  }

  G <- unique(G)
  G2 <- data.table(G, key="ID")

  G2 <- G2[, list(sdw = paste0(sort(unique(unlist(strsplit(sdw, split=", ")))), collapse=", "), 
                  sd = paste0(sort(unique(unlist(strsplit(sd, split=", ")))), collapse=", "))  , by = "ID"]
  G2 <- data.table( G2, key=c("sd", "sdw"))
  G2 <- unique(G2)
  Bloc <- G2[-1,]
  Bloc$ID <- NULL
  N2 <- nrow(Bloc)  
if (N1 == N2)
break
}

输出

阵营$ SDW

[1] "SD1:FIELD, SD1:LUSH, SD23:SWITCH, SD32:TRUMPET, SD384:FR563, SD44:CANCEL, SD567:TREK"                              
[2] "SD12:PAINTING, SD183:WIRE, SD315:MONEY31, SD340:DUST, SD345:FOX, SD345:HOLE, SD387:ROLL, SD387:SPRING, SD42:CRAYON"
[3] "SD39:MATURE, SD455:TOMATO"  

答案 1 :(得分:0)

您可以尝试以下方式:

id <- lapply(d, function(x) sapply(strsplit(x, ":"), "[", 1))
tbl <- table(unlist(id))

分离出ID,并找到多个条目中出现的ID:

repeatIDs <- names(tbl)[tbl>1]
out <- list()

现在,使用以下命令构建包含重复ID的任何压缩列表:

for (i in repeatIDs) {
    ind <- sapply(id, function(x) any(i==x))
    out[[i]] <- paste(unlist(d[ind]), collapse=", ")
}

答案 2 :(得分:0)

我想如果你在Gavin的答案中计算id,然后计算所有intersect(id[[j]],id[[k]]),或者甚至更好:

for (j in unique(unlist(id))) sapply(id,function(k) j%in%k)

将为您提供交叉点(您必须按下该代码产生的TRUE TRUE FALSE...向量)

编辑:所以这里有什么跟进:

id <- lapply(sdin, function(x) sapply(strsplit(x, ":"), "[", 1))
# id is 
# [[1]]
# [1] "SD1"   "SD44"  "SD384" "SD32" 

# [[2]]
# [1] "SD23"  "SD1"   "SD567"

# [[3]]
# [1] "SD42"  "SD345" "SD183"

# [[4]]
# [1] "SD345" "SD340" "SD387"

# [[5]]
# [1] "SD455" "SD39" 

# [[6]]
# [1] "SD12"  "SD315" "SD387"

# [[7]]
# [1] "SD32" "SD1"

idnames<-unique(unlist(id)) 
# [1] "SD1"   "SD44"  "SD384" "SD32"  "SD23"  "SD567" "SD42" 
 # [8] "SD345" "SD183" "SD340" "SD387" "SD455" "SD39"  "SD12" 
# [15] "SD315"

matid<-matrix(NA,nrow=15,ncol=7)
for(k in 1:length(idnames) ) matid[k,] <- unlist(sapply(id, function(j) idnames[k]%in%j))
      # [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]
 # [1,]  TRUE  TRUE FALSE FALSE FALSE FALSE  TRUE
 # [2,]  TRUE FALSE FALSE FALSE FALSE FALSE FALSE
 # [3,]  TRUE FALSE FALSE FALSE FALSE FALSE FALSE
 # [4,]  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE
 # [5,] FALSE  TRUE FALSE FALSE FALSE FALSE FALSE
 # [6,] FALSE  TRUE FALSE FALSE FALSE FALSE FALSE
 # [7,] FALSE FALSE  TRUE FALSE FALSE FALSE FALSE
 # [8,] FALSE FALSE  TRUE  TRUE FALSE FALSE FALSE
 # [9,] FALSE FALSE  TRUE FALSE FALSE FALSE FALSE
# [10,] FALSE FALSE FALSE  TRUE FALSE FALSE FALSE
# [11,] FALSE FALSE FALSE  TRUE FALSE  TRUE FALSE
# [12,] FALSE FALSE FALSE FALSE  TRUE FALSE FALSE
# [13,] FALSE FALSE FALSE FALSE  TRUE FALSE FALSE
# [14,] FALSE FALSE FALSE FALSE FALSE  TRUE FALSE
# [15,] FALSE FALSE FALSE FALSE FALSE  TRUE FALSE

该矩阵的每一行对应于&#34; SDx&#34;值和每个列到输入d列表中的一个列表元素。您应该能够从该表生成维恩图。