Search code examples
rdplyrtidyr

Combining multiple columns in one R


How can I combine multiple all dataframe's columns in just 1 column? , in an efficient way... I mean not using the column names to do it, using dplyr or tidyr on R, cause I have too much columns (10.000+)

For example, converting this data frame

  > Multiple_dataframe
      a    b    c

      1    4    7
      2    5    8
      3    6    9

to

  > Uni_dataframe
     d

     1
     2
     3
     4
     5
     6
     7
     8
     9

I looked around Stack Overflow but without success.

Edit: Here are a answers for a similar question, the difference is there they did not use dplyr.


Solution

  • We can use unlist

    Uni_dataframe <- data.frame(d = unlist( Multiple_dataframe, use.names = FALSE))
    

    Or using dplyr/tidyr (as the question is specific about it)

    library(tidyverse)
    Uni_dataframe <- gather(Multiple_dataframe, key, d) %>%
                                                 select(-key)