Concatenate two columns of a data frame in R
Consider this data frame in R that contains two columns named a and b:
a b
a1 b1
a2 b2
a3 b3
I want to create a list by concatenating columns a and b like this:
a1 b1 a2 b2 a3 b3
How can i do this?
We can transpose ( t
) the dataset and then concatenate ( c
) the output matrix
to getvector
c(t(df1))
#[1] "a1" "b1" "a2" "b2" "a3" "b3"
Or use as.vector
.
as.vector(t(df1))
#[1] "a1" "b1" "a2" "b2" "a3" "b3"
Usually used to convert 'data.frame' to 'vector' unlist
, but it will be written off column by column. This is the reason for migrating the dataset and using c
or as.vector
.
Or list
you can use to get the output as.list
.
as.list(t(df1))