rbind dataframes with a different column name

前端 未结 5 541
伪装坚强ぢ
伪装坚强ぢ 2020-11-27 20:28

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(cla         


        
5条回答
  •  伪装坚强ぢ
    2020-11-27 20:55

    Here is a possible tidyverse solution. I created 3 example dataframes based on your description of your dataframes.

    df1 <- read.table(text ="date,source,medium,campaign,goal1Completions,ad.cost,Goal
    2014-10-01,(direct),(none),(notset),1,0.0000,Vida
    2014-10-01,Master,email,CAFRE,2,0.0000,Vida
    2014-10-01,apeseg,referral,(not set),3,0.0000,vida",sep = ",",header=TRUE) 
    
    df2 <- read.table(text ="date,source,medium,campaign,goal2Completions,ad.cost,Goal
    2014-10-01,(direct),(none),(notset),4,0.0000,Vida
    2014-10-01,Master,email,CAFRE,5,0.0000,Vida
    2014-10-01,apeseg,referral,(not set),6,0.0000,vida",sep = ",",header=TRUE) 
    
    df3 <- read.table(text ="date,source,medium,campaign,goal3Completions,ad.cost,Goal
    2014-10-01,(direct),(none),(notset),7,0.0000,Vida
    2014-10-01,Master,email,CAFRE,8,0.0000,Vida
    2014-10-01,apeseg,referral,(not set),9,0.0000,vida",sep = ",",header=TRUE) 
    
    > df1
            date   source   medium  campaign goal1Completions ad.cost Goal
    1 2014-10-01 (direct)   (none)  (notset)                1       0 Vida
    2 2014-10-01   Master    email     CAFRE                2       0 Vida
    3 2014-10-01   apeseg referral (not set)                3       0 vida
    > df2
            date   source   medium  campaign goal2Completions ad.cost Goal
    1 2014-10-01 (direct)   (none)  (notset)                4       0 Vida
    2 2014-10-01   Master    email     CAFRE                5       0 Vida
    3 2014-10-01   apeseg referral (not set)                6       0 vida
    > df3
            date   source   medium  campaign goal3Completions ad.cost Goal
    1 2014-10-01 (direct)   (none)  (notset)                7       0 Vida
    2 2014-10-01   Master    email     CAFRE                8       0 Vida
    3 2014-10-01   apeseg referral (not set)                9       0 vida
    
    library(dplyr)
    library(tidyselect)
    library(purrr)
    
    bind_rows(df1,df2,df3) %>%
       mutate(goalCompletions = reduce(select_at(.,vars(matches("goal[[:digit:]]+Completions"))),coalesce)) %>%
       select_at(vars(-matches("goal[[:digit:]]+Completions")))
    
            date   source   medium  campaign ad.cost Goal goalCompletions
    1 2014-10-01 (direct)   (none)  (notset)       0 Vida               1
    2 2014-10-01   Master    email     CAFRE       0 Vida               2
    3 2014-10-01   apeseg referral (not set)       0 vida               3
    4 2014-10-01 (direct)   (none)  (notset)       0 Vida               4
    5 2014-10-01   Master    email     CAFRE       0 Vida               5
    6 2014-10-01   apeseg referral (not set)       0 vida               6
    7 2014-10-01 (direct)   (none)  (notset)       0 Vida               7
    8 2014-10-01   Master    email     CAFRE       0 Vida               8
    9 2014-10-01   apeseg referral (not set)       0 vida               9
    

提交回复
热议问题