我喜欢reshape2套餐,因为它让生活变得如此轻松.通常情况下,Hadley在之前的软件包中进行了改进,使代码更精简、运行速度更快.我想我会给tidyr一个旋转,从我读到的我认为gatherreshape2中的melt非常相似.但在阅读了文档之后,我无法让gather人完成melt人所做的相同任务.

Data View

以下是数据视图(本文末尾以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

Code

这是melt码,我try 了gather码.我怎样才能让gathermelt做同样的事情?

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)) 

Desired Output

   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

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")

推荐答案

你的gather行应该是这样的:

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

这表示"收集除teacherpd之外的所有变量,调用新的键列‘variable’和新的值列‘date’."


作为解释,请注意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.

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

R相关问答推荐

在边界外添加注释或标题

用derrr在R中查找组间的重复项

如何调整曲线图中的y轴标签?

合并DFS列表并将索引提取为新列

我正在努力用R计算数据集中的中值逐步距离

2个Rscript.exe可执行文件有什么区别?

根据列A中的差异变异列,其中行由列B中的相对值标识

用约翰逊分布进行均值比较

比较理论阿尔法和经验阿尔法

SHINY:使用JS函数应用的CSS样式显示HTML表格

有没有办法使用ggText,<;Sub>;&;<;sup>;将上标和下标添加到同一元素?

为什么在BASE R中绘制线条时会看到线上的点?

调换行/列并将第一行(原始数据帧的第一列)提升为标题的Tidyr类似功能?

为什么在写入CSV文件时Purrr::Pwalk不起作用

如何根据其他列中的两个条件来计算数据帧中的行之间的差异?

如何使用grepl()在数据帧列表中 Select 特定字符串?

如何在访问之前下载的输入时同时上传和处理所有指定的shiny 输入?

根据排名的顶点属性调整曲线图布局(&Q)

如何在不使用SHINY的情况下将下拉滤镜列表添加到ggploy?

R data.设置函数&;连接中的列值而不使用for循环的表方法?