0

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.

lbevs
  • 11
  • 1
  • 3
  • 1
    Welcome to SO. Please include a reproducible question as suggested here [How to ask1](https://stackoverflow.com/help/minimal-reproducible-example) & [How to ask2](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) include your data (as a dataframe object or use dput("yourdata"), the code you have tried and your expected output. This will make it more likely to get a good answer. – rj-nirbhay May 07 '20 at 22:31

1 Answers1

1
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.

Arnaud Feldmann
  • 761
  • 5
  • 17