R中的日期序列
Sequence of dates in R
我需要病人在医院的所有天数,AufnDat 和 EntlDat 之间的天数。我将 seq 与 mutate 一起使用,如 reprex 中所示,我收到错误:
seq.Date(from = dmy(AufnDat), to = dmy(EntlDat), by = "day") 错误:'from' muss Länge 1 haben
``` r
library(tidyverse)
library(lubridate)
#>
#> Attache Paket: 'lubridate'
#> The following object is masked from 'package:base':
#>
#> date
df <- tibble::tribble(
~PLZ, ~AufnDat, ~EntlDat, ~Stat1,
67067, "22.01.2020", "23.01.2020", "1A",
74936, "21.01.2020", "23.01.2020", "1A",
97999, "21.01.2020", "27.01.2020", "0A",
76669, "15.01.2020", "17.01.2020", "1",
69126, "07.01.2020", "13.01.2020", "0A",
68753, "13.01.2020", "17.01.2020", "1A",
74928, "23.01.2020", "28.01.2020", "2A",
64720, "10.01.2020", "13.01.2020", "1",
48599, "16.01.2020", "22.01.2020", "0A",
68723, "07.01.2020", "13.01.2020", "0A"
)
df <- df %>% mutate(seqdat = seq(from=dmy(AufnDat), to=dmy(EntlDat), by="day"))
#> Error in seq.Date(from = dmy(AufnDat), to = dmy(EntlDat), by = "day"): 'from' muss Länge 1 haben
```
if I try the same for one row without mutate it works
``` r
## this works:
seq(from=dmy(df[1,2]), to=dmy(df[1,3]), by="day")
#> [1] "2020-01-22" "2020-01-23"
```
由 reprex package (v0.3.0)
于 2020 年 2 月 21 日创建
哪里不一样和我错了?
我们可以使用 map2
遍历每个 'Aufndat','EntlDat' 以获得 'Date' 的序列,因为 seq
没有向量化from
、to
。这将是一个 list
列,我们可以 unnest
扩展数据集
library(dplyr)
library(purrr)
library(tidyr)
df %>%
mutate(seqdat = map2(dmy(AufnDat), dmy(EntlDat), seq, by = 'day')) %>%
unnest(c(seqdat))
作为@akruns 答案的替代方案,我们可以将 seqdat
存储为列表列表:
library(dplyr)
library(lubridate)
df_new <- df %>%
mutate(ID = row_number()) %>%
group_by(ID) %>%
mutate(seqdat = list(seq(from=dmy(AufnDat), to=dmy(EntlDat), by="day"))) %>%
ungroup()
df_new 现在是:
df_new
# A tibble: 10 x 6
# Groups: ID [10]
PLZ AufnDat EntlDat Stat1 ID seqdat
<dbl> <chr> <chr> <chr> <int> <list>
1 67067 22.01.2020 23.01.2020 1A 1 <date [2]>
2 74936 21.01.2020 23.01.2020 1A 2 <date [3]>
3 97999 21.01.2020 27.01.2020 0A 3 <date [7]>
4 76669 15.01.2020 17.01.2020 1 4 <date [3]>
5 69126 07.01.2020 13.01.2020 0A 5 <date [7]>
6 68753 13.01.2020 17.01.2020 1A 6 <date [5]>
7 74928 23.01.2020 28.01.2020 2A 7 <date [6]>
8 64720 10.01.2020 13.01.2020 1 8 <date [4]>
9 48599 16.01.2020 22.01.2020 0A 9 <date [7]>
10 68723 07.01.2020 13.01.2020 0A 10 <date [7]>
和
df_new$seqdat[1]
Returns:
[[1]]
[1] "2020-01-22" "2020-01-23"
我需要病人在医院的所有天数,AufnDat 和 EntlDat 之间的天数。我将 seq 与 mutate 一起使用,如 reprex 中所示,我收到错误: seq.Date(from = dmy(AufnDat), to = dmy(EntlDat), by = "day") 错误:'from' muss Länge 1 haben
``` r
library(tidyverse)
library(lubridate)
#>
#> Attache Paket: 'lubridate'
#> The following object is masked from 'package:base':
#>
#> date
df <- tibble::tribble(
~PLZ, ~AufnDat, ~EntlDat, ~Stat1,
67067, "22.01.2020", "23.01.2020", "1A",
74936, "21.01.2020", "23.01.2020", "1A",
97999, "21.01.2020", "27.01.2020", "0A",
76669, "15.01.2020", "17.01.2020", "1",
69126, "07.01.2020", "13.01.2020", "0A",
68753, "13.01.2020", "17.01.2020", "1A",
74928, "23.01.2020", "28.01.2020", "2A",
64720, "10.01.2020", "13.01.2020", "1",
48599, "16.01.2020", "22.01.2020", "0A",
68723, "07.01.2020", "13.01.2020", "0A"
)
df <- df %>% mutate(seqdat = seq(from=dmy(AufnDat), to=dmy(EntlDat), by="day"))
#> Error in seq.Date(from = dmy(AufnDat), to = dmy(EntlDat), by = "day"): 'from' muss Länge 1 haben
```
if I try the same for one row without mutate it works
``` r
## this works:
seq(from=dmy(df[1,2]), to=dmy(df[1,3]), by="day")
#> [1] "2020-01-22" "2020-01-23"
```
由 reprex package (v0.3.0)
于 2020 年 2 月 21 日创建哪里不一样和我错了?
我们可以使用 map2
遍历每个 'Aufndat','EntlDat' 以获得 'Date' 的序列,因为 seq
没有向量化from
、to
。这将是一个 list
列,我们可以 unnest
扩展数据集
library(dplyr)
library(purrr)
library(tidyr)
df %>%
mutate(seqdat = map2(dmy(AufnDat), dmy(EntlDat), seq, by = 'day')) %>%
unnest(c(seqdat))
作为@akruns 答案的替代方案,我们可以将 seqdat
存储为列表列表:
library(dplyr)
library(lubridate)
df_new <- df %>%
mutate(ID = row_number()) %>%
group_by(ID) %>%
mutate(seqdat = list(seq(from=dmy(AufnDat), to=dmy(EntlDat), by="day"))) %>%
ungroup()
df_new 现在是:
df_new
# A tibble: 10 x 6
# Groups: ID [10]
PLZ AufnDat EntlDat Stat1 ID seqdat
<dbl> <chr> <chr> <chr> <int> <list>
1 67067 22.01.2020 23.01.2020 1A 1 <date [2]>
2 74936 21.01.2020 23.01.2020 1A 2 <date [3]>
3 97999 21.01.2020 27.01.2020 0A 3 <date [7]>
4 76669 15.01.2020 17.01.2020 1 4 <date [3]>
5 69126 07.01.2020 13.01.2020 0A 5 <date [7]>
6 68753 13.01.2020 17.01.2020 1A 6 <date [5]>
7 74928 23.01.2020 28.01.2020 2A 7 <date [6]>
8 64720 10.01.2020 13.01.2020 1 8 <date [4]>
9 48599 16.01.2020 22.01.2020 0A 9 <date [7]>
10 68723 07.01.2020 13.01.2020 0A 10 <date [7]>
和
df_new$seqdat[1]
Returns:
[[1]]
[1] "2020-01-22" "2020-01-23"