简体   繁体   中英

is there an R function to combine Columns of the same name by creating more rows but not losing data?

I am looking for a way to combine columns but not remove the data. I have a 1x30 daratrame. The columns names repeat throughout the df. I would like to combine the data so that all columns named the same are in the same column, but with multiple rows. I am trying to pipe this in and have been trying merge() but I can not seem to get it to work.

EX: 1

I would like to have the El Salvador cols combined but add a row so r1 has 18 and r2 has 2036, etc. for all the columns.

df <- as.data.frame(as.list(rnorm(40)))
names(df) <- sample(c("a","b","c","d","e"),size = 40,replace=TRUE)
df

this is something like your data? (This is not normal because dataframes usually don't look like that.

Then

split(as.numeric(df[1,]),colnames(df))

will produce a list by colnames

granted that all the columns have the same length, as.data.frame would then produce a data.frame.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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