简体   繁体   中英

Create sequence of dates in R with pipe %>%

I am trying to create a sequence of dates from a raw string.

library(tidyverse)
tmp <- "1900-01-01 3000-01-01"

This yields a vector of length 2:

tmp %>% str_split(., " ", simplify = T) %>% as.vector() %>% as.Date

I need to create a sequence of dates based on the results of that pipe. My workflow demands use of the pipe operator.

tmp %>% str_split(., " ", simplify = T) %>% as.vector() %>% as.Date %>% seq(from = magrittr::extract(., 1), to = magrittr::extract(., 2), by = "1 day")

This implementation fails, and I'm unsure why. But I've also tried this, to no avail:

tmp %>% str_split(., " ", simplify = T) %>% as.vector() %>% as.Date %>% magrittr::extract(., 1):magrittr::extract(., 2)

Does seq() not support piping? What am I missing?

We could block with {}

library(stringr)
library(dplyr)
tmp %>% 
   str_split(., " ", simplify = TRUE) %>%
   as.vector() %>% 
   as.Date %>% 
   {seq(from = magrittr::extract(., 1), 
     to = magrittr::extract(., 2), by = "1 day")}

You can use fread to read the text as a space-delimited file with two columns.

In the CRAN version of data.table, you have to use tz = 'UTC' to get it to parse dates. In the dev version that is not necessary.

tmp <- "1900-01-01 3000-01-01"
library(data.table)

fread(text = tmp, tz = 'UTC')[, seq(V1, V2, by = '1 day')]
#>     [1] "1900-01-01" "1900-01-02" "1900-01-03" "1900-01-04" "1900-01-05"
#>     [6] "1900-01-06" "1900-01-07" "1900-01-08" "1900-01-09" "1900-01-10"
#>    [11] "1900-01-11" "1900-01-12" "1900-01-13" "1900-01-14" "1900-01-15"
#>    [ reached 'max' / getOption("max.print") ]

Created on 2021-12-07 by the reprex package (v2.0.1)

Or similar to @akrun's answer:

library(rlang)
library(stringr)

tmp %>% 
  str_split(" ", simplify = TRUE) %>%
  as.Date %>% 
  exec(seq, !!!., by = '1 day')

The first uses magrittr %$%, the second only uses %>% and the third uses only base R with the base R pipe and the 4th, which is tied with the first as the shortest, uses only base R without pipes.

library(magrittr)

tmp %>% read.table(text = ., colClasses = "Date") %$% seq(V1, V2, 1)

tmp %>% read.table(text = ., colClasses = "Date") %>% with(seq(V1, V2, 1))

tmp |>
  textConnection() |>
  read.table(colClasses = "Date") |>
  with(seq(V1, V2, 1))

with(read.table(text = tmp, colClasses = "Date", seq(V1, V2, "day"))

Note

tmp <- "1900-01-01 1900-01-04"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM