This question already has an answer here:
- Combine data in many row into a columnn 3 answers
Apologies if this question is a repeat but I couldn't find it.
I am looking to reshape a data frame in the form (read in from read_bulk):
"name.a", 5
"name.a", 4
"name.a", 1
"name.b", 2
"name.b", 3
"name.b", 2
"name.c", 1
"name.c", 5
"name.c", 6
into the form:
5, 4, 1
2, 3, 2
1, 5, 6
The real data frame consists of thousands of numbers for each name, I do not know the number in each but they are all equal. Each name is to be a different row in the final form.
I attempted this with reshape but couldn't seem to get this to work, any thoughts?
using other libraries:
data:
After looking through the responses I think I found a quicker way (simpler). Using the data I managed to get it to work with:
You can do the transformation using
dplyr
andreshape2
:Provided the format is always the same, something like this in base R?
Explanation:
unlist(df[, 2])
turns the entries indf[, 2]
into a vector, then reformat into amatrix
withncol = 3
columns, and finally cast intodata.frame
.Sample data