首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >比较聚集(tidyr)和熔化(reshape2)

比较聚集(tidyr)和熔化(reshape2)
EN

Stack Overflow用户
提问于 2014-10-24 03:51:19
回答 3查看 25.7K关注 0票数 69

我喜欢reshape2包,因为它让生活变得如此简单。通常情况下,Hadley在他以前的包中进行了改进,使代码能够简化,运行速度更快。我想我应该试一试tidyr,从我读到的内容来看,我认为gatherreshape2中的melt非常相似。但在阅读文档后,我无法让gather执行与melt相同的任务。

数据视图

下面是数据视图(post结束时dput格式的实际数据):

代码语言:javascript
复制
  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

代码

这是我在gather中尝试的melt形式的代码。怎样才能让gather做和melt一样的事情呢?

代码语言:javascript
复制
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)) 

所需的输出

代码语言:javascript
复制
   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

Data

代码语言:javascript
复制
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")
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-10-24 03:54:32

您的gather行应该如下所示:

代码语言:javascript
复制
dat %>% gather(variable, date, -teacher, -pd)

这表示“收集除teacherpd之外的所有变量,将新键列称为'variable‘,将新值列称为’date‘。”

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

代码语言:javascript
复制
 ...: 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.

因为这是一个省略号,所以要收集的列的规范是作为单独的(裸名称)参数给出的。我们希望收集除teacherpd之外的所有列,因此我们使用-

票数 89
EN

Stack Overflow用户

发布于 2019-09-24 21:40:19

在tidyr 1.0.0中,这项任务是通过更灵活的pivot_longer()完成的。

等价的语法是

代码语言:javascript
复制
library(tidyr)
dat %>% pivot_longer(cols = -c(teacher, pd), names_to = "variable", values_to = "date")

相应地,它表示“透视除teacherpd之外的所有内容,将新变量列称为" variable”,将新值列称为"date“。

请注意,长数据首先按透视后的前一个数据帧的列的顺序返回,而不像gather那样按新变量列的顺序返回。要重新排列生成的tibble,请使用dplyr::arrange()

票数 7
EN

Stack Overflow用户

发布于 2022-01-01 07:52:38

我的解决方案

代码语言:javascript
复制
    dat%>%
    gather(!c(teacher,pd),key=variable,value=date)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26536251

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档