问题描述
我有一个data.frames列表,每个数据框架中有多个列。每个数据框架具有相同的结构。
另外我有另一个列表,多个data.frames。
I have a list of data.frames with multiple columns in each of the data.frames. Each data.frame has the same structure.In addition I have another list with multiple data.frames.
我们假设这两个列表是
firstlist <- list(a=data.frame(AA=5:1,
BB=1:5),
b=data.frame(AA=5:1,
BB=1:5),
c=data.frame(AA=5:1,
BB=1:5))
secondlist <- list(a=data.frame(AA=1:10,
BB=c(0,0,1,0,0,1,1,0,0,0)),
b=data.frame(AA=1:10,
BB=c(0,1,0,0,0,0,1,0,0,0)),
c=data.frame(AA=1:10,
BB=c(1,0,0,0,0,1,1,0,0,0)))
现在我想将列CC添加到firstlist中的所有data.frames,并将它们相应地从第二列中的列BB中的值填充。
Now I want to add column CC to all data.frames in firstlist and fill them accordingly to the values in column BB from the secondlist.
问题是:我需要检查来自第一个列表的AA或BB中的行是否包含在第二个列表中的AA中的值,并在该列表中填入新列的列表中的值从BB在第二列表。
The problem is: I need to check if the row in AA or BB from firstlist contains the value from AA in secondlist and fill the new column CC in firstlist with the value from BB in secondlist.
上面的示例数据的预期结果将是:
The expected result with the example data above would be:
> firstlist
$a
AA BB CC
1 5 1 0
2 4 2 0
3 3 3 1
4 2 4 0
5 1 5 0
$b
AA BB CC
1 5 1 0
2 4 2 1
3 3 3 0
4 2 4 1
5 1 5 0
$c
AA BB CC
1 5 1 1
2 4 2 0
3 3 3 0
4 2 4 0
5 1 5 1
我需要使用For循环或还有其他方式吗?
Do I need to use a For loop or is there any other way?
更新:
请参阅,以及所有数据类型的boolen数据和。
UPDATE:See Thell's solution for boolen data and eddie's solution for all datatypes.
提前谢谢!
推荐答案
如果CC是真正的布尔值...
If CC is truly boolean...
f <- function(a,b) cbind( a, CC=b$BB[ match( a$AA, b$AA ) ] |
b$BB[ match( a$BB, b$AA ) ] )
mapply( f, firstlist, secondlist, SIMPLIFY=F )
直线,快速,保持名字...
Straight-forward, quick, and keeps names...
基准的例子vs lapply版本::
benchmark of example vs lapply version::
Unit: milliseconds
expr min lq median uq max neval
this mapply 1.726471 1.840671 1.870504 1.939473 13.88875 100
Arun's lapply 2.930061 3.048110 3.134402 3.209786 14.61630 100
这篇关于两个列表与多列数据框架的条件匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!