rbind具有不同列名称的数据框 [英] rbind dataframes with a different column name

查看:409
本文介绍了rbind具有不同列名称的数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有12个数据框,每个数据框包含6列:5个具有相同的名称,1个不同。然后当我调用 rbind()时得到

I've 12 data frames, each one contains 6 columns: 5 have the same name, 1 is different. Then when I call rbind() I get:

Error in match.names(clabs, names(xi)) : 
  names do not match previous names

不符的列是:目标1完成。有12个目标完成...它们是: goal1Completions, goal2Completions, goal3Completions ...等等。

The column that differs is: "goal1Completions". There are 12 goalsCompletions... they are: "goal1Completions", "goal2Completions", "goal3Completions"... and so on.

我能想到的最好的方法是:将每个数据帧中的每一列重命名为 GoalsCompletions。然后使用 rbind()。

The best way I can think of is: renaming every column in every data frame to "GoalsCompletions" and then using "rbind()".

有没有更简单的方法?

在Google O上找到该包: < a href = http://cran.r-project.org/web/packages/gtools/gtools.pdf rel = nofollow noreferrer> gtools 。它具有称为: smartbind的功能。但是,在使用smartbind()之后,我想用 View()查看数据框,我的R会话崩溃了……

Look on Google O found this package: "gtools". It has a function called: "smartbind". However, after using smartbind() i want to see the the data frame with "View()", my R session crashes...

我的数据(第一个示例数据框):

My data (an example of the first data frame):

       date      source     medium   campaign   goal1Completions    ad.cost           Goal
1   2014-10-01  (direct)    (none)   (not set)          0           0.0000            Vida
2   2014-10-01   Master      email     CAFRE            0           0.0000            Vida
3   2014-10-01  apeseg      referral (not set)          0           0.0000            Vida


推荐答案

您可以使用 rbindlist 列名。使用@LyzandeR的数据

You could use rbindlist which takes different column names. Using @LyzandeR's data

library(data.table) #data.table_1.9.5
rbindlist(list(a,b))
#            a         b
# 1: 0.8403348 0.1579255
# 2: 0.4759767 0.8182902
# 3: 0.8091875 0.1080651
# 4: 0.9846333 0.7035959
# 5: 0.2153991 0.8744136
# 6: 0.7604137 0.9753853
# 7: 0.7553924 0.1210260
# 8: 0.7315970 0.6196829
# 9: 0.5619395 0.1120331
#10: 0.5711995 0.7252631



更新



基于12个数据集(即 Goal1_Costo, Goal2_Costo,..., Goal12_Costo),

Update

Based on the object names of the 12 datasets (i.e. 'Goal1_Costo', 'Goal2_Costo',..., 'Goal12_Costo'),

 nm1 <- paste(paste0('Goal', 1:12), 'Costo', sep="_")
 #or using `sprintf`
 #nm1 <- sprintf('%s%d_%s', 'Goal', 1:12, 'Costo')
 rbindlist(mget(nm1))

这篇关于rbind具有不同列名称的数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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