比较聚集(tidyr)以融化(reshape2)


67

我喜欢reshape2软件包,因为它使生活变得轻松自如。通常,Hadley在其先前的软件包中进行了改进,以启用简化的,运行速度更快的代码。我想我会给tidyr一抡,并从我读我认为gather是非常相似meltreshape2。但是在阅读了文档之后,我无法gather完成相同的任务melt

资料检视

这是数据视图(实际数据以dput帖子结尾的形式显示):

  teacher yr1.baseline     pd yr1.lesson1 yr1.lesson2 yr2.lesson1 yr2.lesson2 yr2.lesson3
1       3      1/13/09 2/5/09      3/6/09     4/27/09     10/7/09    11/18/09      3/4/10
2       7      1/15/09 2/5/09      3/3/09      5/5/09    10/16/09    11/18/09      3/4/10
3       8      1/27/09 2/5/09      3/3/09     4/27/09     10/7/09    11/18/09      3/5/10

这是melt我尝试的时尚代码gather。我gather该怎么做melt

library(reshape2); library(dplyr); library(tidyr)

dat %>% 
   melt(id=c("teacher", "pd"), value.name="date") 

dat %>% 
   gather(key=c(teacher, pd), value=date, -c(teacher, pd)) 

期望的输出

   teacher     pd     variable     date
1        3 2/5/09 yr1.baseline  1/13/09
2        7 2/5/09 yr1.baseline  1/15/09
3        8 2/5/09 yr1.baseline  1/27/09
4        3 2/5/09  yr1.lesson1   3/6/09
5        7 2/5/09  yr1.lesson1   3/3/09
6        8 2/5/09  yr1.lesson1   3/3/09
7        3 2/5/09  yr1.lesson2  4/27/09
8        7 2/5/09  yr1.lesson2   5/5/09
9        8 2/5/09  yr1.lesson2  4/27/09
10       3 2/5/09  yr2.lesson1  10/7/09
11       7 2/5/09  yr2.lesson1 10/16/09
12       8 2/5/09  yr2.lesson1  10/7/09
13       3 2/5/09  yr2.lesson2 11/18/09
14       7 2/5/09  yr2.lesson2 11/18/09
15       8 2/5/09  yr2.lesson2 11/18/09
16       3 2/5/09  yr2.lesson3   3/4/10
17       7 2/5/09  yr2.lesson3   3/4/10
18       8 2/5/09  yr2.lesson3   3/5/10

数据

dat <- structure(list(teacher = structure(1:3, .Label = c("3", "7", 
    "8"), class = "factor"), yr1.baseline = structure(1:3, .Label = c("1/13/09", 
    "1/15/09", "1/27/09"), class = "factor"), pd = structure(c(1L, 
    1L, 1L), .Label = "2/5/09", class = "factor"), yr1.lesson1 = structure(c(2L, 
    1L, 1L), .Label = c("3/3/09", "3/6/09"), class = "factor"), yr1.lesson2 = structure(c(1L, 
    2L, 1L), .Label = c("4/27/09", "5/5/09"), class = "factor"), 
        yr2.lesson1 = structure(c(2L, 1L, 2L), .Label = c("10/16/09", 
        "10/7/09"), class = "factor"), yr2.lesson2 = structure(c(1L, 
        1L, 1L), .Label = "11/18/09", class = "factor"), yr2.lesson3 = structure(c(1L, 
        1L, 2L), .Label = c("3/4/10", "3/5/10"), class = "factor")), .Names = c("teacher", 
    "yr1.baseline", "pd", "yr1.lesson1", "yr1.lesson2", "yr2.lesson1", 
    "yr2.lesson2", "yr2.lesson3"), row.names = c(NA, -3L), class = "data.frame")

11
您可能对reshape2和tidyr + dplyr软件包的比较感兴趣。我以空气质量示例和炸薯条示例为例,比较了reshape2melt()和dcast()函数与tidyr collect()和spread()函数以及dplyr group_by()和summarise()函数结合使用的情况。
Paul Rougieux

Answers:


87

您的gather行应如下所示:

dat %>% gather(variable, date, -teacher, -pd)

这说:“收集除teacher和以外的所有变量pd,将新键列称为'变量',将新值列称为'日期'。


作为说明,请注意help(gather)页面中的以下内容:

 ...: 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.

由于这是省略号,因此要收集的列的规范将作为单独的(裸名)参数给出。我们希望收集除teacher和以外的所有列pd,因此我们使用-


3
该语法现在很合理。我只是没看过。感谢您的出色回应。
泰勒·林克

@BenBolker我更喜欢。它可以帮助我将事物归类在大脑中,也可以减少击键次数。
泰勒·林克

7

在tidyr 1.0.0中,此任务可以更灵活地完成pivot_longer()

等效语法为

library(tidyr)
dat %>% pivot_longer(cols = -c(teacher, pd), names_to = "variable", values_to = "date")

相应地,“将除teacher和之外的所有内容再旋转一次pd,将新变量列称为”变量”,将新值列称为”日期”。

请注意,与相比,长数据按已旋转的前一个数据帧的列的顺序先返回,而不同于gather,数据按新变量列的顺序返回。要重新排列最终的小标题,请使用dplyr::arrange()

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.