我知道如何将两个 id.vars 与melt. 这很简单:
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
Run Code Online (Sandbox Code Playgroud)
但是我怎样才能做同样的事情(使用两个 id.vars 或key)gather呢?
gather(data, key, value, ..., na.rm = FALSE, convert = FALSE,
factor_key = FALSE)
Run Code Online (Sandbox Code Playgroud)
我只能设法使用一个key。
您还可以指定要在使用-符号时保留的变量:
gather(x, variable, value, -c(subject,time))
Run Code Online (Sandbox Code Playgroud)