Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dplyr: Two keys in gather

Tags:

r

dplyr

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.

like image 427
alberto Avatar asked Mar 11 '23 07:03

alberto


2 Answers

You can also specify the variables you want to keep as they are using the - sign:

gather(x, variable, value, -c(subject,time))
like image 178
Tamas Nagy Avatar answered Mar 24 '23 08:03

Tamas Nagy


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
like image 44
akrun Avatar answered Mar 24 '23 09:03

akrun