Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining multiple columns in one R [duplicate]

Tags:

r

dplyr

tidyr

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.

like image 352
Forever Avatar asked Jul 15 '17 03:07

Forever


1 Answers

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)
like image 182
akrun Avatar answered Sep 28 '22 06:09

akrun