使用不同的列名 rbind 数据框 [英] rbind dataframes with a different column name

查看:31
本文介绍了使用不同的列名 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

不同的列是:goal1Completions".有 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 上查找此软件包:"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天全站免登陆