R将函数应用于带有lapply的列表

时间:2018-02-11 10:17:48

标签: r list lapply

我有三个列表,List1包含标识符,List2包含逗号分隔的字符串,可能是List1中的项目,List3包含数字(一些测量分数)。

List1=c("Object1","Object2",......,"Objectn")
List2=c("Object1","Object2,Object3","Object4","Object5","Object6", .... )
List3=c("0.90","0,80",....)

所有列表都有相同的长度。

我想对List1中的每个项目做什么,对于List2中的每个项目,检查交集是否为空,并增加分数。

我可以迭代地执行此操作,但由于我的列表太长,我想用lapply做但却失败了。任何帮助将不胜感激。

FinalScoreList="",

for(i in 1:length(List1)){

  score=0

   for(j in 1:length(List2)){


      if(length(intersect(List1[[i]], 
             as.list(unlist(strsplit(as.character(List2[j]),',')))))>0) {

            score=score+as.double(List3[j])

        }

      }

     FinalScoreList=c(FinalScoreList,score)

   }

4 个答案:

答案 0 :(得分:0)

我认为这是你所追求的事情:

List1=c("Object1","Object2", "0.70")
List2=c("Object1","Object2", "Object3")
List3=c("0.90","0,80", "0.70")

# Make a list of lists
All_Lists = list(
  "List1" = List1,
  "List2" = List2,
  "List3" = List3
)

# Create a dataframe listing all pairwise combinations of the lists
intersect_df <- data.frame(t(combn(names(All_Lists), 2)))

# Add a new column to this dataframe indicating the length of the intersection
# between each pair of lists
intersect_df$count <- apply(intersect_df, 1, function(r) length(intersect(All_Lists[[r[1]]], All_Lists[[r[2]]])))

输出:

> intersect_df
     X1    X2 count
1 List1 List2     2
2 List1 List3     1
3 List2 List3     0

因此输出中的每一行都指定了两个列表(X1X2)的组合,列count表示这两个列表之间的交集长度。

答案 1 :(得分:0)

首先,我不建议给出名称&#34; List&#34; (List1,List2,List3 ...)到非列表的项目。 第二,因为你想要&#34; List3&#34;数字元素从一开始就做。我创建了以下示例:

library(dplyr)
List1=c("Object1","Object2","Object3","Object4","Object5","Object6","Object7","Object8")
List2=c("Object3","Object4","Object5","Object6","Object7","Object8","Object9","Object10")
List3=c("0.90","0.80","0.70","0.60","0.50","0.40","0.30","0.20")%>%as.numeric

现在您的代码几乎没有任何改动,我们得到FinalScoreList

FinalScoreList=c()

for(i in 1:length(List1)){

  score=0

  for(j in 1:length(List2)){

    if(length(intersect(List1[[i]], as.list(unlist(strsplit(as.character(List2[j]),',')))))>0) {
      score=score+List3[j]
    }
  }
  FinalScoreList=c(FinalScoreList,score)
}
> FinalScoreList
[1] 0.0 0.0 0.9 0.8 0.7 0.6 0.5 0.4

我们可以在不使用以下代码循环的情况下获得相同的结果:

df=data.frame(List1,List2,List3)
df$Matches<-0
matches0<-grep(List1,pattern=paste(intersect(List2,List1),collapse="|"))
matches1<-grep(List2,pattern=paste(intersect(List2,List1),collapse="|"))
df$Matches[matches0]<-List3[matches1]
> df$Matches
[1] 0.0 0.0 0.9 0.8 0.7 0.6 0.5 0.4

答案 2 :(得分:0)

您可以在循环之前执行List2的拆分,这已经加快了速度。此外,当您从空向量FinalScoreList开始时,R必须在每个步骤中增长它,这使得它也变慢。

这是一个嵌套lapply / sapply的解决方案 - 调用:

List2 <- lapply(List2, function(x) unlist(strsplit(x, split = ",")))

FinalScoreList <- lapply(List1, function(x) {
  indicator <- sapply(List2, function(y) x %in% y) 
  sum(List3[indicator])
})

unlist(FinalScoreList)

正如@Antonis所说,你应该将你的List3向量存储为数字向量。

数据

List1 <- paste0("Object", 1:10)
List2 <- c("Object1", "Object6,Object5", "Object2,Object1", "Object7", 
           "Object6,Object8", "Object5,Object9", "Object4,Object2", 
           "Object3,Object8", "Object2,Object6", "Object10,Object3")
List3 <- runif(10)

答案 3 :(得分:0)

谢谢你们。

现在假设List1与List2具有相同的性质,即项目可以是连接字符串。并且也可以有不同的长度。

我在List1上做了lapply strsplit,但我仍然在FinalScoreList中获得了NA。

List1 <- c("Object1", "Object7,Object5", "Object2,Object1")


List2 <- c("Object1", "Object6,Object5", "Object0,Object1", "Object7", 
           "Object6,Object8", "Object5,Object9", "Object4,Object2", 
           "Object3,Object8", "Object2,Object3", "Object10,Object3")


List3 <- runif(10)

List2 <- lapply(List2, function(x) unlist(strsplit(x, split = ",")))


List1 <- lapply(List1, function(x) unlist(strsplit(x, split = ",")))

FinalScoreList <- lapply(List1, function(x) {
  indicator <- sapply(List2, function(y) {x %in% y}) 
  sum(List3[indicator])
})

unlist(FinalScoreList)

[1] 1.595639 NA NA