It will be a trick, I suppose.
Explanation
df[,names(df) == 'a'] will select all columns named a
unlist converts the above columns into one single vector
unname will remove some street unname names given to these vectors.
unique(names(df)) will give you unique column names in df
sapply apply the built-in function to all unique(names(df)) values
> df abcabcabc 1 0 1 2 5 6 2 0 1 2 2 1 2 3 6 7 3 1 2 3 3 2 3 4 7 8 4 2 3 4 > sapply(unique(names(df)), function(x) unname(unlist(df[,names(df)==x]))) abc [1,] 0 1 2 [2,] 1 2 3 [3,] 2 3 4 [4,] 5 6 2 [5,] 6 7 3 [6,] 7 8 4 [7,] 0 1 2 [8,] 1 2 3 [9,] 2 3 4
source share