合并命令在R [英] Merge Command in R

查看:134
本文介绍了合并命令在R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在R中使用merge命令,我试图找出如何使用SUFFIX参数。

I have been playing around with the merge command in R and am trying to figure out how to use the SUFFIX parameter. The online documentation does not do a very good job at explaining it.

我想做的是导入一些csv文件:

What I'd like to do is import some csv files:

data1<-read.csv("fileA", header=T)
data2<-read.csv("fileB", header=T)

然后使用merge命令合并它们。然而,我想要一些变量真正合并,而其他变量,具有相同的名称,以标记他们来自的文件。例如,如果一个NAME变量存在于我的两个电子表格中,那么我想要像正常一样合并它们,但是如果一个GRADE变量出现,它将被更改为GRADE.fileA和GRADE .fileB。我已经能够获得GRADE.x和GRADE.y,但我更喜欢更有用的标签上这些变量。任何帮助,这将不胜感激。谢谢。

And then use the merge command to merge them. However, I would like for some variables to be truly merged, while other variables that hold the same name to be labeled by the file they came from. So for instance, if a "NAME" variable existed in both of my spreadsheets, then I'd like for them to be merged as normal, but if a "GRADE" variable showed up, it would be changed to GRADE.fileA and GRADE.fileB. I am already able to get GRADE.x and GRADE.y, but I would prefer more useful labels on these variables. Any help on this would be appreciated. Thank you.

推荐答案

我认为这应该可以工作:

I think this should work:

merged.df <- merge(data1, data2, by='NAME', suffixes=c('.fileA', '.fileB'))

这篇关于合并命令在R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆