Binding columns with similar column names in the same dataframe in R

后端 未结 5 1150
挽巷
挽巷 2021-01-05 06:01

I have a data frame that looks somewhat like this:

df <- data.frame(0:2, 1:3, 2:4, 5:7, 6:8, 2:4, 0:2, 1:3, 2:4)
colnames(df) <- rep(c(\'a\', \'b\', \         


        
5条回答
  •  清酒与你
    2021-01-05 06:19

    I'm not at the computer now, so can't test this, but... this might work:

    do.call(cbind, 
         lapply(names(df) function(x) do.call(rbind, df[, names(df) == x])) )
    

提交回复
热议问题