合并两个数据框并删除重复的列

时间:2022-04-17 22:51:01

I want to cbind two data frames and remove duplicated columns. For example:

我想cbind两个数据框并删除重复的列。例如:

df1 <- data.frame(var1=c('a','b','c'), var2=c(1,2,3))
df2 <- data.frame(var1=c('a','b','c'), var3=c(2,4,6))

cbind(df1,df2) #this creates a data frame in which column var1 is duplicated

I want to create a data frame with columns var1, var2 and var3, in which column var2 is not repeated.

我想创建一个包含var1,var2和var3列的数据框,其中不重复列var2。

2 个解决方案

#1


8  

merge will do that work.

合并将完成这项工作。

try:

merge(df1, df2)

#2


1  

In case you inherit someone else's dataset and end up with duplicate columns somehow and want to deal with them, this is a nice way to do it:

如果您继承了其他人的数据集,并以某种方式最终得到重复的列并想要处理它们,这是一个很好的方法:

for (name in unique(names(testframe))) {
  if (length(which(names(testframe)==name)) > 1) {
    ## Deal with duplicates here. In this example
    ## just print name and column #s of duplicates:
    print(name)
    print(which(names(testframe)==name))
  }
}

#1


8  

merge will do that work.

合并将完成这项工作。

try:

merge(df1, df2)

#2


1  

In case you inherit someone else's dataset and end up with duplicate columns somehow and want to deal with them, this is a nice way to do it:

如果您继承了其他人的数据集,并以某种方式最终得到重复的列并想要处理它们,这是一个很好的方法:

for (name in unique(names(testframe))) {
  if (length(which(names(testframe)==name)) > 1) {
    ## Deal with duplicates here. In this example
    ## just print name and column #s of duplicates:
    print(name)
    print(which(names(testframe)==name))
  }
}