我有一个由data.frames组成的列表:
> list1
$HSP90AB1
lineNum count
HSP90AB1 1 1
INMT 2 0
CKB 3 0
NR2E1 4 1
ME3 5 0
FAM162A 6 0
KIRREL2 7 0
$INMT
lineNum count
HSP90AB1 1 1
INMT 2 0
CKB 3 0
NR2E1 4 0
ME3 5 0
FAM162A 6 0
KIRREL2 7 1
$CKB
lineNum count
HSP90AB1 1 1
INMT 2 0
CKB 3 0
NR2E1 4 0
ME3 5 1
FAM162A 6 0
KIRREL2 7 0
$NR2E1
lineNum count
HSP90AB1 1 0
INMT 2 0
CKB 3 0
NR2E1 4 0
ME3 5 0
FAM162A 6 0
KIRREL2 7 0
$ME3
lineNum count
HSP90AB1 1 0
INMT 2 1
CKB 3 0
NR2E1 4 0
ME3 5 0
FAM162A 6 0
KIRREL2 7 0
$FAM162A
lineNum count
HSP90AB1 1 1
INMT 2 0
CKB 3 0
NR2E1 4 0
ME3 5 0
FAM162A 6 0
KIRREL2 7 0
$KIRREL2
lineNum count
HSP90AB1 1 0
INMT 2 0
CKB 3 0
NR2E1 4 0
ME3 5 0
FAM162A 6 0
KIRREL2 7 0
我想获得以下结果:首先,将列表中每个元素的列1
中的count
的数量相加,然后将该次数的总和相加此元素的名称在列表的其余元素中有1
,例如对于list1[[2]]
($ INMT),第一步将是:2(有两个1
in count
列),第二步是:1(1
中有一个与INMT
对应的$ME3
。列表的一个组成部分通过使用Reduce
指出了我,但如果我这样做:
Reduce('+', list1)
lineNum count
HSP90AB1 7 4
INMT 14 1
CKB 21 0
NR2E1 28 1
ME3 35 1
FAM162A 42 0
KIRREL2 49 1
我只是从我想要的第二步,任何想法?
由于
PS还有另一个需要解决的重要问题,在第一步中只应计算与名称不匹配的元素,仅在$HSP90AB1
(list1[[1]]
)的第一步中的示例中应计入与1
对应的NR2E1
,因为其他1
对应HSP90AB1
与list1[[1]]
的名称匹配。
所需的输出如下:
HSP90AB1 4
INMT 3
CKB 2
NR2E1 1
ME3 2
FAM162A 1
KIRREL2 1
以下是dput(list1)
:
structure(list(HSP90AB1 = structure(list(lineNum = 1:7, count = c(1L, 0L, 0L, 1L, 0L, 0L, 0L)),
.Names = c("lineNum", "count"), row.names = c("HSP90AB1","INMT", "CKB", "NR2E1", "ME3","FAM162A", "KIRREL2"), class = "data.frame"),
INMT = structure(list(lineNum = 1:7, count = c(1L, 0L, 0L,
0L, 0L, 0L, 1L)), .Names = c("lineNum", "count"), row.names = c("HSP90AB1",
"INMT", "CKB", "NR2E1", "ME3", "FAM162A", "KIRREL2"), class = "data.frame"),
CKB = structure(list(lineNum = 1:7, count = c(1L, 0L, 0L,
0L, 1L, 0L, 0L)), .Names = c("lineNum", "count"), row.names = c("HSP90AB1",
"INMT", "CKB", "NR2E1", "ME3", "FAM162A", "KIRREL2"), class = "data.frame"),
NR2E1 = structure(list(lineNum = 1:7, count = c(0L, 0L, 0L,
0L, 0L, 0L, 0L)), .Names = c("lineNum", "count"), row.names = c("HSP90AB1",
"INMT", "CKB", "NR2E1", "ME3", "FAM162A", "KIRREL2"), class = "data.frame"),
ME3 = structure(list(lineNum = 1:7, count = c(0L, 1L, 0L,
0L, 0L, 0L, 0L)), .Names = c("lineNum", "count"), row.names = c("HSP90AB1",
"INMT", "CKB", "NR2E1", "ME3", "FAM162A", "KIRREL2"), class = "data.frame"),
FAM162A = structure(list(lineNum = 1:7, count = c(1L, 0L,
0L, 0L, 0L, 0L, 0L)), .Names = c("lineNum", "count"), row.names = c("HSP90AB1",
"INMT", "CKB", "NR2E1", "ME3", "FAM162A", "KIRREL2"), class = "data.frame"),
KIRREL2 = structure(list(lineNum = 1:7, count = c(0L, 0L,
0L, 0L, 0L, 0L, 0L)), .Names = c("lineNum", "count"), row.names = c("HSP90AB1",
"INMT", "CKB", "NR2E1", "ME3", "FAM162A", "KIRREL2"), class = "data.frame")), .Names = c("HSP90AB1", "INMT", "CKB", "NR2E1", "ME3", "FAM162A", "KIRREL2"))
答案 0 :(得分:2)
这应该这样做。一个重要的步骤是通过清空每个列表元素的不需要的项来修改列表:
list2 <- Map(function(x, exclude) {x[exclude, "count"] <- 0; x},
list1, names(list1))
然后你可以这样做:
count1 <- sapply(list2, with, sum(count))
count2 <- Reduce(`+`, list2)$count
count1 + count2
# HSP90AB1 INMT CKB NR2E1 ME3 FAM162A KIRREL2
# 4 3 2 1 2 1 1