Merge columns in R

后端 未结 3 834
清酒与你
清酒与你 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.

提交回复
热议问题