tidyr 从宽到长?

tidyr wide to long?

我正在尝试学习如何使用 tidyr 将宽数据转换为长数据。假设我的数据如下所示:

df1 <- data.frame(V1=c(1.5,7.4), 
                  V2=c(6.7,8.8),
                  sim=c(1,2))

我想把自己改造成这样:

df2 <- data.frame(sim=c(1,1,2,2),
                  Value=c(1.5,6.7,7.4,8.8))

我认为收集是我必须使用的功能,但我不理解帮助文件。这就是我现在拥有的:

df1 %>%
   gather(key=sim, value=V1:V2)

错误显示 "Error: Invalid column specification"

谢谢!

尝试

library(tidyr)
df1 %>%
    gather(sim1, Value, V1:V2) %>% 
    select(-sim1) %>%
    arrange(sim)
#    sim Value
#1   1   1.5
#2   1   6.7
#3   2   7.4
#4   2   8.8

根据?gather

  gather(data, key, value, ..., na.rm = FALSE, convert = FALSE)

key,value: Names of key and value columns to create in output.

...: Specification of columns to gather. Use bare variable names. Select all variables between x and z with ‘x:z’, exclude y with ‘-y’. For more options, see the select documentation.