简体   繁体   English

组合2个具有不同列名的数据帧

[英]combine 2 dataframes having different column names

In R ,I have 2 data frames both having different column name. 在R中,我有2个数据帧都具有不同的列名。 I want to combine the rows of each data frame according to the column number. 我想根据列号组合每个数据帧的行。 the dataframes i have is as follows 我拥有的数据帧如下

> d1
  X.0.52..V2 X.0.52..V4
1        ABT        700
2        AMD       9600
3        AMG        600
4       AGCO        800

> d2
  X.52.96..V2 X.52.96..V4
1        COMS      162193
2         MMM      419645
3          SE      146343
4        ADCT       62609
5         TCC        6623

I want the following dataframe: 我想要以下数据帧:

 >d3

       ticker        value
 1        ABT         700
 2        AMD        9600
 3        AMG         600
 4       AGCO         800
 5       COMS      162193
 6        MMM      419645
 7         SE      146343
 8       ADCT       62609
 9        TCC        6623

what is the code i need to use? 我需要使用的代码是什么?

If it's this simple I'd be inclined to use: 如果这很简单,我倾向于使用:

colnames(d1) <- colnames(d2) <- c("ticker", "value")
rbind.data.frame(d1, d2)

If your actual situation is as simple as this, you can easily match the names from the two: 如果您的实际情况如此简单,您可以轻松匹配两者中的名称:

names(df2) <- names(df1)

Then rbind them together: 然后将它们rbind在一起:

df.both <- rbind(df1, df2)

and give the dataframe the names you want: 并为数据框提供所需的名称:

names(df.both) <- c("ticker", "value")

# > df.both
# ticker  value
# 1     ABT    700
# 2     AMD   9600
# 3     AMG    600
# 4    AGCO    800
# 11   COMS 162193
# 21    MMM 419645
# 31     SE 146343
# 41   ADCT  62609
# 5     TCC   6623

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM