I know how to use two id.vars with melt
. It's straightforward:
x = data.frame(subject = c("John", "Mary"),
time = c(1,1),
age = c(33,35),
weight = c(90, 67),
height = c(2,2))
melt(x, id.vars = c('subject', 'time'), measure.vars = c('age', 'weight', 'height'))
# subject time variable value
#1 John 1 age 33
#2 Mary 1 age 35
#3 John 1 weight 90
#4 Mary 1 weight 67
#5 John 1 height 2
#6 Mary 1 height 2
But how can I do the same (using two id.vars, or key
) with gather
?
gather(data, key, value, ..., na.rm = FALSE, convert = FALSE,
factor_key = FALSE)
I can only manage to use one key
.
You can also specify the variables you want to keep as they are using the -
sign:
gather(x, variable, value, -c(subject,time))
We can do this with either specifying the columns separately or if it is in the same order use :
or have column indexes
library(tidyr)
gather(x, variable, value, age:height)
# subject time variable value
#1 John 1 age 33
#2 Mary 1 age 35
#3 John 1 weight 90
#4 Mary 1 weight 67
#5 John 1 height 2
#6 Mary 1 height 2
which would give the same output as melt
from reshape2
melt(x, id.vars = c('subject', 'time'), measure.vars = c('age', 'weight', 'height'))
# subject time variable value
#1 John 1 age 33
#2 Mary 1 age 35
#3 John 1 weight 90
#4 Mary 1 weight 67
#5 John 1 height 2
#6 Mary 1 height 2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With