Merge columns in R

后端 未结 3 833
清酒与你
清酒与你 2021-01-25 00:36

I want to merge two columns of a data frame into one long column using R. I have a reproducible data below:

data<-data.frame(x=c(4,5,6,7,7,7),y=c(3,4,5,6,7,7))         


        
相关标签:
3条回答
  • 2021-01-25 00:57

    You can convert the dataframe to matrix and then to vector :

    data.frame(new = c(as.matrix(data)))
    
    #   new
    #1    4
    #2    5
    #3    6
    #4    7
    #5    7
    #6    7
    #7    3
    #8    4
    #9    5
    #10   6
    #11   7
    #12   7
    

    Or get the data in long format :

    tidyr::pivot_longer(data, cols = everything())
    

    and keep only the value column.

    0 讨论(0)
  • 2021-01-25 01:14

    We can use unlist

    data.frame(new = unlist(data))
    
    0 讨论(0)
  • 2021-01-25 01:23

    dplyr::bind_rows(new$x, new$y) works as well

    0 讨论(0)
提交回复
热议问题