2015-09-29 29 views
1

我必须使用不同行数的数据帧df_a和df_b。这是我的数据结构的一个例子。如果满足条件,则将某个值添加到行中 - R

DF_A

id ident var 
1 Test1 
2 Test1 
3 Test2 
4 Test1 
5 Test3 

DF_B

id ident var 
1 Test1 26 
2 Test2 59 

现在我想的DF_B是$ var复制到DF_A $ VAR,但只有当IDENT匹配。

结果必须是这样的:

DF_A

id ident var 
1 Test1 26 
2 Test1 26 
3 Test2 59 
4 Test1 26 
5 Test3 NA 

我不太知道如何做到这一点 - 可能有人帮助吗?

+3

http://stackoverflow.com/questions/1299871/how-to-join-merge- data-frames-inner-left-right-right?lq = 1 – beetroot

回答

1

使用dplyr包,这是简单的:

result = left_join(df_a, df_b, by = 'ident') 

这将然而,拷贝过来的一些冗余列。要清理这些了,使用方法:

result = select(result, id = id.x, ident, var = var.y) 
+0

对我很好用:) –

2

我们可以使用joindata.table。我们将第一个'data.frame'转换为'data.table'(setDT(df_a)),使用on = 'ident'加入'df_b'。

library(data.table)#v1.9.6+ 
setDT(df_a)[df_b, var := i.var, on = 'ident'][] 
# id ident var 
#1: 1 Test1 26 
#2: 2 Test1 26 
#3: 3 Test2 59 
#4: 4 Test1 26 
#5: 5 Test3 NA 

注:在上述解决方案中,我删除了'df_a'的空'var'列。

编辑:基于@Aruns的评论。


或者,我们可以使用来自base Rmatch得到的数字指标,并用它来摆脱“DF_B”相应的“变种”。即使我们在'df_a'中有一个空的'var'列,这个方法也可以工作。

df_a$var <- df_b$var[match(df_a$ident, df_b$ident)] 
+1

作品也很棒:) –

+0

@Arun谢谢,我之前没有得到语法。 – akrun

3

使用您的数据:

#I have removed the var column as, 1) it is blank in your case 
#and 2) it will be filled in any way 
df_a <- read.table(header=T, text='id ident 
1 Test1 
2 Test1 
3 Test2 
4 Test1 
5 Test3') 

df_b <- read.table(header=T, text='id ident var 
1 Test1 26 
2 Test2 59') 

这是基础R:

#df_a stays as it is since you need all the columns 
#from df_b we just need indent for the link and var to be added 
#the by argument indicates the "link" column 
#all.x=TRUE states that all columns from df_a will be kept in the result 
merge(df_a, df_b[c('ident','var')], by='ident', all.x=TRUE) 

    ident id var 
1 Test1 1 26 
2 Test1 2 26 
3 Test1 4 26 
4 Test2 3 59 
5 Test3 5 NA 
相关问题