I have a dataframe with the following columns:
id condition trial a0 a1 ... a7 b1 b2 ... b7
1 1 1 23 56 ... 52 87 56 ... 98
1 1 2 57 87 ... 87 55 52 ... 52
1 1 3 23 56 ... 52 87 56 ... 98
2 1 1 23 56 ... 82 97 56 ... 68
a and b are values measured at times 0, 1 ... 7. Measure a has values at 0 and b has not.
I want to have it in the format:
id condition trial time measureA measureB
1 1 1 0 23 NA
1 1 2 0 57 NA
1 1 3 0 23 NA
...
1 1 1 1 56 87
1 1 2 1 87 55
...
In other words, I have a 7 second time series for 2 measures spread over 2*7 columns, and I wanted to "collect" them in a single column. One of them also has a value at time 0.
This is what I have tried so far:
I used the functiongather
x = gather(x, timeA, measureA, -c(1:3, 4:11))
x = gather(x, timeB, measureB, -c(1:3, 10,11))
and could put the dataframe in the following format:
id condition trial timeA measureA timeB measureB
0 12 1 45
0 52 1 85
1 51 2 55
1 45 2 92
2 45 3 15
2 52 3 54
I wanted to use merge
to merge timeA and timeB, but it does not work because the data frame is too big.
I used the code:
merge(x[1:5], x[6:7], by.x="timeA", by.y = "timeB")
I get the error message
Error: cannot allocate vector of size 40980.3 Gb
Is there any way to achieve what I want to do?