我有一个包含64个数据帧的列表。 数据帧1和数据帧5必须具有相同的行名称。 与2和6,3和7相同,依此类推。 我能够运行一个for循环并创建一个新列表,但某些工作不正常:我最终得到的行数不正确。过滤存储在列表中的数据帧的行并创建新列表
这里一个简单的例子来重现:
# Create dataframes and store in list
dfA <- data.frame(v1=c(1:6), v2=c("x1","x2","x3","x4","x5","x6"))
dfB <- data.frame(v1=c(1:6), v2=c("x1","x2","x3","x4","x5","x6"))
dfC <- data.frame(v1=c(1:5), v2=c("x1","x2","x3","x4","x5"))
dfD <- data.frame(v1=c(1:4), v2=c("x1","x2","x3","x4"))
example_dataframes = list(dfA, dfB, dfC, dfD)
# These vectors give the order of the process
vectorA = c(1,2)
vectorB = c(3,4)
# Create new list and start for loop
filtered_dataframes = list()
for (i in vectorA) {
for (j in vectorB) {
df1 = example_dataframes[[i]]
df2 = example_dataframes[[j]]
test = intersect(df1$v2, df2$v2)
filtered_dataframes[[i]] <- df1[which(df1$v2 %in% test),]
filtered_dataframes[[j]] <- df2[which(df2$v2 %in% test),]
}
}
在这个例子中,我希望能获得:
sapply(filtered_dataframes, nrow)
> 5 4 5 4
我想你只需要一个for-cycle来遍历vectorA和vectorB的索引,而不是一个两层递归的for-cycle。 – mt1022