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))
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.
We can use unlist
data.frame(new = unlist(data))
dplyr::bind_rows(new$x, new$y)
works as well