合并两个数据框并仅保留匹配的列

oop

(编辑)我有两个带有不同列的数据框。我想将它们串联起来,只保留我想要的列。

#this is the data
df1 = data.frame(Id = c(129,109), Product = c('nutella', 'crepes'), 
             sales= c(1000000, 200000), ratings = c(5,3), Company=c('a','b'))
df2 = data.frame(Id = c(154,198), Product = c('cheesecake', 'oreo'), 
             sales= c(150000, 3000000), Taxpaid = c(120, 3000), 
             Company=c('c','d'))


#Desired Output:

    Id     Product       
    129    nutella     
    109    crepes      
    154    cheesecake  
    198    oreo        
阿克伦

我们可以做一个intersect两个数据集和列的rbind数据集

nm1 <- intersect(names(df1), names(df2))
rbind(df1[nm1], df2[nm1])

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章