alberto alberto - 3 months ago 20
R Question

dplyr: Two keys in gather

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
.

Answer

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
Comments