We can take the transpose (t
) of the dataset and then use c
to get a vector output
c(t(df1))
#[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.