52

我有一个宽格式的数据框,在不同的日期范围内进行了重复测量。在我的示例中,有三个不同的时期,它们都有对应的值。例如,第一次测量 ( Value1) 是在从DateRange1Start到期间测量的DateRange1End

ID DateRange1Start DateRange1End Value1 DateRange2Start DateRange2End Value2 DateRange3Start DateRange3End Value3
1 1/1/90 3/1/90 4.4 4/5/91 6/7/91 6.2 5/5/95 6/6/96 3.3 

我希望将数据重塑为长格式,以便对 DateRangeXStart 和 DateRangeXEnd 列进行分组。因此,原表中的 1 行变为新表中的 3 行:

ID DateRangeStart DateRangeEnd Value
1 1/1/90 3/1/90 4.4
1 4/5/91 6/7/91 6.2
1 5/5/95 6/6/96 3.3

我知道必须有一种方法可以使用reshape2/ melt/ recast/来做到这一点tidyr,但我似乎无法弄清楚如何以这种特定方式将多组度量变量映射到单组值列。

4

8 回答 8

41
reshape(dat, idvar="ID", direction="long", 
             varying=list(Start=c(2,5,8), End=c(3,6,9), Value=c(4,7,10)),
             v.names = c("DateRangeStart", "DateRangeEnd", "Value") )
#-------------
    ID time DateRangeStart DateRangeEnd Value
1.1  1    1          1/1/90        3/1/90    4.4
1.2  1    2          4/5/91        6/7/91    6.2
1.3  1    3          5/5/95        6/6/96    3.3

(根据 Josh 的建议添加了 v.names。)

于 2012-09-17T20:31:51.047 回答
35

data.tablemelt函数可以融合成多个列。使用它,我们可以简单地做:

require(data.table)
melt(setDT(dat), id=1L,
     measure=patterns("Start$", "End$", "^Value"), 
     value.name=c("DateRangeStart", "DateRangeEnd", "Value"))

#    ID variable DateRangeStart DateRangeEnd Value
# 1:  1        1         1/1/90       3/1/90   4.4
# 2:  1        2         4/5/91       6/7/91   6.2
# 3:  1        3         5/5/95       6/6/96   3.3

或者,您也可以通过列位置引用三组度量列:

melt(setDT(dat), id = 1L, 
     measure = list(c(2,5,8), c(3,6,9), c(4,7,10)), 
     value.name = c("DateRangeStart", "DateRangeEnd", "Value"))
于 2015-02-28T20:30:21.060 回答
23

从1.0.0pivot_longer()版开始,使用tidyr包的功能可以将具有多个值/度量列的宽格式重塑为长格式。

这优于之前的 tidyr 策略gather()than spread()(参见@AndrewMacDonald 的回答),因为不再删除属性(在下面的示例中,日期仍然是日期,数字仍然是数字)。

library("tidyr")
library("magrittr")

a <- structure(list(ID = 1L, 
                    DateRange1Start = structure(7305, class = "Date"), 
                    DateRange1End = structure(7307, class = "Date"), 
                    Value1 = 4.4, 
                    DateRange2Start = structure(7793, class = "Date"),
                    DateRange2End = structure(7856, class = "Date"), 
                    Value2 = 6.2, 
                    DateRange3Start = structure(9255, class = "Date"), 
                    DateRange3End = structure(9653, class = "Date"), 
                    Value3 = 3.3),
               row.names = c(NA, -1L), class = c("tbl_df", "tbl", "data.frame"))

pivot_longer()(counterpart: pivot_wider()) 与gather(). 但是,它提供了额外的功能,例如多值列。只有一个值列,宽数据集的所有列名将进入一个长列,名称为names_to. 对于多个值列,names_to可能会收到多个新名称。

如果所有列名都遵循特定的模式,例如Start_1End_1Start_2等,这是最简单的。因此,我在第一步中重命名了这些列。

(names(a) <- sub("(\\d)(\\w*)", "\\2_\\1", names(a)))
#>  [1] "ID"               "DateRangeStart_1" "DateRangeEnd_1"  
#>  [4] "Value_1"          "DateRangeStart_2" "DateRangeEnd_2"  
#>  [7] "Value_2"          "DateRangeStart_3" "DateRangeEnd_3"  
#> [10] "Value_3"

pivot_longer(a, 
             cols = -ID, 
             names_to = c(".value", "group"),
             # names_prefix = "DateRange",
             names_sep = "_")
#> # A tibble: 3 x 5
#>      ID group DateRangeEnd DateRangeStart Value
#>   <int> <chr> <date>       <date>         <dbl>
#> 1     1 1     1990-01-03   1990-01-01       4.4
#> 2     1 2     1991-07-06   1991-05-04       6.2
#> 3     1 3     1996-06-06   1995-05-05       3.3

或者,可以使用提供更精细控制的枢轴规范来完成重塑(参见下面的链接):

spec <- a %>%
    build_longer_spec(cols = -ID) %>%
    dplyr::transmute(.name = .name,
                     group = readr::parse_number(name),
                     .value = stringr::str_extract(name, "Start|End|Value"))

pivot_longer(a, spec = spec)

reprex 包(v0.2.1)于 2019 年 3 月 26 日创建

另见:https ://tidyr.tidyverse.org/articles/pivot.html

于 2019-03-20T13:56:52.010 回答
19

这是使用tidyr. 这是它的功能的一个有趣用例extract_numeric(),我用来从列名中提取组

library(dplyr)
library(tidyr)

a <- read.table(textConnection("
ID DateRange1Start DateRange1End Value1 DateRange2Start DateRange2End Value2 DateRange3Start DateRange3End Value3
1 1/1/90 3/1/90 4.4 4/5/91 6/7/91 6.2 5/5/95 6/6/96 3.3 
"),header=TRUE)

a %>%
  gather(variable,value,-ID) %>%
  mutate(group = extract_numeric(variable)) %>%
  mutate(variable =  gsub("\\d","",x = variable)) %>%
  spread(variable,value)

  ID group DateRangeEnd DateRangeStart Value
1  1     1       3/1/90         1/1/90   4.4
2  1     2       6/7/91         4/5/91   6.2
3  1     3       6/6/96         5/5/95   3.3
于 2014-06-23T20:05:20.490 回答
8

两个额外的选项(带有一个多行的示例数据框,以更好地显示代码的工作):

1) 以 R 为底:

l <- lapply(split.default(d[-1], cumsum(grepl('Start$', names(d)[-1]))),
            setNames, c('DateRangeStart','DateRangeEnd','Value'))
data.frame(ID = d[,1], do.call(rbind, l), row.names = NULL)

这使:

  ID DateRangeStart DateRangeEnd Value
1  1         1/1/90       3/1/90   4.4
2  2         1/2/90       3/2/90   6.1
3  1         4/5/91       6/7/91   6.2
4  2         4/6/91       6/8/91   3.2
5  1         5/5/95       6/6/96   3.3
6  2         5/5/97       6/6/98   1.3

2)与tidyverse

library(dplyr)
library(purrr)

split.default(d[-1], cumsum(grepl('Start$', names(d)[-1]))) %>%
  map_dfr(~set_names(., c('DateRangeStart','DateRangeEnd','Value'))) %>% 
  bind_cols(ID = rep(d$ID, nrow(.)/nrow(d)), .)

3)使用sjmisc-package:

library(sjmisc)
to_long(d, keys = 'group',
        values = c('DateRangeStart','DateRangeEnd','Value'), 
        c('DateRange1Start','DateRange2Start','DateRange3Start'),
        c('DateRange1End','DateRange2End','DateRange3End'),
        c('Value1','Value2','Value3'))[,-2]

如果您还想要一个组/时间列,您可以将上述方法调整为:

1) 以 R 为底:

l <- lapply(split.default(d[-1], cumsum(grepl('Start$', names(d)[-1]))),
            setNames, c('DateRangeStart','DateRangeEnd','Value'))
data.frame(ID = d[,1],
           group = rep(seq_along(l), each = nrow(d)),
           do.call(rbind, l), row.names = NULL)

这使:

  ID group DateRangeStart DateRangeEnd Value
1  1     1         1/1/90       3/1/90   4.4
2  2     1         1/2/90       3/2/90   6.1
3  1     2         4/5/91       6/7/91   6.2
4  2     2         4/6/91       6/8/91   3.2
5  1     3         5/5/95       6/6/96   3.3
6  2     3         5/5/97       6/6/98   1.3

2)与tidyverse

split.default(d[-1], cumsum(grepl('Start$', names(d)[-1]))) %>%
  map_dfr(~set_names(., c('DateRangeStart','DateRangeEnd','Value'))) %>% 
  bind_cols(ID = rep(d$ID, nrow(.)/nrow(d)),
            group = rep(1:(nrow(.)/nrow(d)), each = nrow(d)), .)

3)使用sjmisc-package:

library(sjmisc)
to_long(d, keys = 'group', recode.key = TRUE,
        values = c('DateRangeStart','DateRangeEnd','Value'), 
        c('DateRange1Start','DateRange2Start','DateRange3Start'),
        c('DateRange1End','DateRange2End','DateRange3End'),
        c('Value1','Value2','Value3'))

使用数据:

d <- read.table(text = "ID DateRange1Start DateRange1End Value1 DateRange2Start DateRange2End Value2 DateRange3Start DateRange3End Value3
1 1/1/90 3/1/90 4.4 4/5/91 6/7/91 6.2 5/5/95 6/6/96 3.3
2 1/2/90 3/2/90 6.1 4/6/91 6/8/91 3.2 5/5/97 6/6/98 1.3", header = TRUE, stringsAsFactors = FALSE)
于 2017-11-26T07:02:26.287 回答
2

使用回收:

data.frame(ID = d[, 1],
           DateRangeStart = unlist(d[, -1][, c(TRUE, FALSE, FALSE)]),
           DateRangeEnd  = unlist(d[, -1][, c(FALSE, TRUE, FALSE)]),
           Value =  unlist(d[, -1][, c(FALSE, FALSE, TRUE)]))
于 2018-04-10T11:00:01.563 回答
0

你不需要任何花哨的东西;基本R功能会做。

a <- read.table(textConnection("
ID DateRange1Start DateRange1End Value1 DateRange2Start DateRange2End Value2 DateRange3Start DateRange3End Value3
1 1/1/90 3/1/90 4.4 4/5/91 6/7/91 6.2 5/5/95 6/6/96 3.3 
"),header=TRUE)
b1 <- a[,c(1:4)]; b2 <- a[,c(1,5:7)]; b3 <- a[,c(1,8:10)]
colnames(b1) <- colnames(b2) <- colnames(b3) <- c("ID","DateRangeStart","DateRangeEnd","Value")
b <- rbind(b1,b2,b3)
于 2012-09-17T20:29:28.557 回答
0

中的另一个解决方案tidyverse,它利用names_patterntidyr::pivot_longer().

names_pattern采用与 相同的规范extract(),包含匹配组 ( ()) 的正则表达式。

作为不需要初步字符串操作的单个命令,这可能会比@hplieninger答案有所改进。

解决方案

library(tidyverse)


# ...
# Code to generate dataset 'ds'.
# ...


ds %>% pivot_longer(
  # Target only those columns names with a numeric index; possibly suffixed by "Start"
  # or "End".
  cols = matches("^(.*)(\\d+)(Start|End)?$"),
  # Break each name into its prefix, index, and suffix.
  names_pattern = "^(.*)(\\d+)(Start|End)?$",
  # Pivot by index and reassemble the other components.
  names_to = c(".value", "group_id", ".value")
)

您可以进一步将代码简化为单行代码,如下所示

pivot_longer(ds, !ID, names_pattern = "^(.*)(\\d+)(Start|End)?$", names_to = c(".value", NA, ".value"))

其中!ID仅将每一列(但ID)作为分组变量;并在示例输出中NA省略组索引 ( )。group_id

结果

给定一个ds喜欢你的样本数据集

ds <- structure(
  list(
    ID = 1L,
    DateRange1Start = structure(7305, class = "Date"), 
    DateRange1End = structure(7307, class = "Date"),
    Value1 = 4.4, 
    DateRange2Start = structure(7793, class = "Date"),
    DateRange2End = structure(7856, class = "Date"), 
    Value2 = 6.2,
    DateRange3Start = structure(9255, class = "Date"), 
    DateRange3End = structure(9653, class = "Date"),
    Value3 = 3.3
  ),
  row.names = c(NA, -1L),
  class = c("tbl_df", "tbl", "data.frame")
)

该解决方案应产生以下结果

# A tibble: 3 x 5
     ID group_id DateRangeStart DateRangeEnd Value
  <int> <chr>    <date>         <date>       <dbl>
1     1 1        1990-01-01     1990-01-03     4.4
2     1 2        1991-05-04     1991-07-06     6.2
3     1 3        1995-05-05     1996-06-06     3.3

或者对于简化的命令:

# A tibble: 3 x 4
     ID DateRangeStart DateRangeEnd Value
  <int> <date>         <date>       <dbl>
1     1 1990-01-01     1990-01-03     4.4
2     1 1991-05-04     1991-07-06     6.2
3     1 1995-05-05     1996-06-06     3.3
于 2022-01-10T19:55:18.840 回答