I want to move an arbitrary number of dataframe rows to columns, not as key-value pairs (like with tidyr::pivot_wider), just as new columns.
So here's a dataframe
df <- data.frame(A = c(1, 2, 3),
B = c(4, 5, 6),
C = c(7, 8, 9),
ID = c("ID", "ID", "ID"))
I can use c like this, but I have to specify each row, which I'd like to avoid. I'm not concerned about the column naming convention. X1, X2 etc. is fine.
l <- c(df[,1], df[,2], df[,3], df[,4])
data.frame(matrix(unlist(l), ncol=length(l), byrow=T))
X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13 X14 X15 X16
1 1 2 3 4 5 6 7 8 9 10 11 12 ID ID ID ID
This also leaves duplicated values in X13, X14, X15, X16, which I'd also like to avoid.
Is there a better way, ideally either in base or dplyr/purrr and without a for loop?
