Following this question I got a very strange behaviour with base
reshape:
Reshaping wide to long with multiple values columns
Here, it works:
df <- data.frame(id=c("a", "b", "c", "d"),
blabla=c("aaa", "bbb", "ccc", "ddd"),
age.t0=c(7,8,9,10),
height.t0=c(120.2, 130.2, 112.1, 109.5),
weight.t0=c(31,32,33,34),
age.t1=c(8,9,10,11),
height.t1=c(125.7, 132.4, 118.2, 114.6),
weight.t1=c(331,332,333,334))
> df
id blabla age.t0 height.t0 weight.t0 age.t1 height.t1 weight.t1
1 a aaa 7 120.2 31 8 125.7 331
2 b bbb 8 130.2 32 9 132.4 332
3 c ccc 9 112.1 33 10 118.2 333
4 d ddd 10 109.5 34 11 114.6 334
df.l <- reshape(df, direction='long',
varying=c("age.t0", "height.t0", "weight.t0", "age.t1", "height.t1", "weight.t1"),
timevar='var',
times=c('t0', 't1'),
v.names=c('age', 'height', "weight"),
idvar='id')
> df.l
id blabla var age height weight
a.t0 a aaa t0 7 120.2 31
b.t0 b bbb t0 8 130.2 32
c.t0 c ccc t0 9 112.1 33
d.t0 d ddd t0 10 109.5 34
a.t1 a aaa t1 8 125.7 331
b.t1 b bbb t1 9 132.4 332
c.t1 c ccc t1 10 118.2 333
d.t1 d ddd t1 11 114.6 334
But if I change the v.names
value weight
to growth
in reshape
, the columns are inverted...
df.l <- reshape(df, direction='long',
varying=c("age.t0", "height.t0", "weight.t0", "age.t1", "height.t1", "weight.t1"),
timevar='var',
times=c('t0', 't1'),
v.names=c('age', 'height', "growth"), ## Change is here. weight --> growth
idvar='id')
> df.l
id blabla var age height growth
a.t0 a aaa t0 7 31 120.2
b.t0 b bbb t0 8 32 130.2
c.t0 c ccc t0 9 33 112.1
d.t0 d ddd t0 10 34 109.5
a.t1 a aaa t1 8 331 125.7
b.t1 b bbb t1 9 332 132.4
c.t1 c ccc t1 10 333 118.2
d.t1 d ddd t1 11 334 114.6
The same happens if the variables names are changed directly in df
.
df <- data.frame(id=c("a", "b", "c", "d"),
blabla=c("aaa", "bbb", "ccc", "ddd"),
age.t0=c(7,8,9,10),
height.t0=c(120.2, 130.2, 112.1, 109.5),
growth.t0=c(31,32,33,34), ## weight --> growth
age.t1=c(8,9,10,11),
height.t1=c(125.7, 132.4, 118.2, 114.6),
growth.t1=c(331,332,333,334)) ## weight --> growth
> df
id blabla age.t0 height.t0 growth.t0 age.t1 height.t1 growth.t1
1 a aaa 7 120.2 31 8 125.7 331
2 b bbb 8 130.2 32 9 132.4 332
3 c ccc 9 112.1 33 10 118.2 333
4 d ddd 10 109.5 34 11 114.6 334
df.l <- reshape(df, direction='long',
varying=c("age.t0", "height.t0", "growth.t0",
"age.t1", "height.t1", "growth.t1"), ## weight --> growth
timevar='var',
times=c('t0', 't1'),
v.names=c('age', 'height', "growth"),
idvar='id')
> df.l
id blabla var age height growth
a.t0 a aaa t0 7 31 120.2
b.t0 b bbb t0 8 32 130.2
c.t0 c ccc t0 9 33 112.1
d.t0 d ddd t0 10 34 109.5
a.t1 a aaa t1 8 331 125.7
b.t1 b bbb t1 9 332 132.4
c.t1 c ccc t1 10 333 118.2
d.t1 d ddd t1 11 334 114.6
So, this is super strange. Also, if you replace growth
in v.names
with, e.g. a
or b
, columns are inverted, but not with e.g. i
or q
.