Gio Circo Gio Circo - 1 year ago 112
R Question

Unlist a data frame by rows, not columns

A relatively simple question, but the answer seems to have eluded me. Currently, I have a data frame which looks similar to this:

0 0 0 1 1
0 1 0 1 1
2 1 1 0 3

I'm trying to turn this into a single line of data, by rows. I used the
function, and it did what I wanted, but gave them to me by columns. It gave me this:


but what I want is this:


I apologize if this seems like a silly question, but I'm still a novice with R. Any help (or referrals to functions which might help me process this) would be greatly appreciated.

Answer Source

We can take the transpose (t) of the dataset and then use c to get a vector output

 #[1] 0 0 0 1 1 0 1 0 1 1 2 1 1 0 3

By doing transpose, we convert the 'data.frame' to 'matrix'. In both data.frame or matrix, unlist/c operations happen columnwise. So, transposing swaps the columns for rows and viceversa and we get the expected result.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download