Reputation: 91
Consider this data frame in R which contains two columns named a and b:
a b
a1 b1
a2 b2
a3 b3
I want to produce a list by merging columns a and b in this way:
a1 b1 a2 b2 a3 b3
How can i do this?
Upvotes: 3
Views: 144
Reputation: 886938
We can transpose (t
) the dataset and then concatenate (c
) the matrix
output to get a vector
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, for converting a 'data.frame' to 'vector', unlist
is used, but it will unlist by column wise. That is the reason for transposing the dataset and using c
or as.vector
.
Or to get list
output, as.list
can be used.
as.list(t(df1))
Upvotes: 3