I want to reshape/ rearrange a dataset, that is stored as a data.frame with 2 columns:
- id (non-unique, i.e. can repeat over several rows) --> stored as character
- value --> stored as numeric value (range 1:3)
Sample data:
id <- as.character(1001:1003)
val_list <- data.frame(sample(1:3, size=12, replace=TRUE))
have <- data.frame(cbind(rep(id, 4), val_list))
colnames(have) <- c("id", "values")
have <- have %>% arrange(id)
This gives me the following output:
id values
1 1001 2
2 1001 2
3 1001 2
4 1001 3
5 1002 2
6 1002 3
7 1002 2
8 1002 2
9 1003 1
10 1003 3
11 1003 1
12 1003 2
What I want:
want <- data.frame(cbind(have[1:4, 2],
have[5:8, 2],
have[9:12, 2]))
colnames(want) <- id
Output of want:
1001 1002 1003
1 2 2 1
2 2 3 3
3 2 2 1
4 3 2 2
My original dataset has >1000 variables "id" and >50 variables "value". I want to chunk/ slice the dataset get a new data.frame where each "id" variable will represent one column listing its "value" variable content.
It is possible to solve it via a loop, but I want to have the vectorized solution. If possible with base R as "one-liner", but other solutions also appreciated.