combine 2 dataframes having different column names

前端 未结 2 1402
一个人的身影
一个人的身影 2021-01-21 13:12

In R ,I have 2 data frames both having different column name. I want to combine the rows of each data frame according to the column number. the dataframes i have is as follows<

相关标签:
2条回答
  • 2021-01-21 13:24

    If it's this simple I'd be inclined to use:

    colnames(d1) <- colnames(d2) <- c("ticker", "value")
    rbind.data.frame(d1, d2)
    
    0 讨论(0)
  • 2021-01-21 13:35

    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:

    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
    
    0 讨论(0)
提交回复
热议问题