Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 循环日期间隔以创建多个变量_R_Loops_Tidyverse_Lubridate - Fatal编程技术网

R 循环日期间隔以创建多个变量

R 循环日期间隔以创建多个变量,r,loops,tidyverse,lubridate,R,Loops,Tidyverse,Lubridate,假设我有以下数据集: library(lubridate) library(tidyverse) df <- data.frame(date1 = c("2011-09-18", "2013-03-06", "2013-08-08"), date2 = c("2012-02-18", "2014-03-06", "2015-02-03")) df$date1 <- as.Date(parse_date_time

假设我有以下数据集:

    library(lubridate)
    library(tidyverse)
    df <- data.frame(date1 = c("2011-09-18", "2013-03-06", "2013-08-08"),
                     date2 = c("2012-02-18", "2014-03-06", "2015-02-03"))
    df$date1 <- as.Date(parse_date_time(df$date1, "ymd"))
    df$date2 <- as.Date(parse_date_time(df$date2, "ymd"))
    df
    #        date1      date2
    # 1 2011-09-18 2012-02-18
    # 2 2013-03-06 2014-03-06
    # 3 2013-08-08 2015-02-03
我可以手动执行以下操作:

#is 2011 associated with dates
df$y_2011 <- if_else(year(df$date1) == 2011, 1, 0, as.numeric(NA))
#is 2014 associated with dates
df$y_2014 <- if_else(between(2014, year(df$date1), year(df$date2)), 1, 0, as.numeric(NA))

#is particular date (2014-04-01) within interval
df$y_1st_2014 <- if_else(between("2014-04-01", df$date1, df$date2), 1, 0, as.numeric(NA))
2011年是否与日期相关
df$y_2011以下是创建与日期范围相关的年份矩阵的解决方案:

library(lubridate)
library(tidyr)
library(dplyr)
df <- data.frame(date1 = c("2011-09-18", "2013-03-06", "2013-08-08"),
                 date2 = c("2012-02-18", "2014-03-06", "2015-02-03"))
df$date1 <- as.Date(parse_date_time(df$date1, "ymd"))
df$date2 <- as.Date(parse_date_time(df$date2, "ymd"))

#identify the years associated with each row.
df$year<-sapply(1:nrow(df), function(i){
  paste(seq(as.numeric(format(df$date1[i], "%Y")), 
            as.numeric(format(df$date2[i], "%Y"))), collapse = ",")})

#separate and convert to wide format
df %>% separate_rows( year, sep=",") %>% 
  mutate(value=1) %>%
  spread(key=year, value=value, fill=0)

#        date1      date2 2011 2012 2013 2014 2015
# 1 2011-09-18 2012-02-18    1    1    0    0    0
# 2 2013-03-06 2014-03-06    0    0    1    1    0
# 3 2013-08-08 2015-02-03    0    0    1    1    1
库(lubridate)
图书馆(tidyr)
图书馆(dplyr)
df
#particular date, 1st of April of each year
b <- seq(as.Date("2011-04-01"), by = "year", length.out = 5)
b
#[1] "2011-01-01" "2012-01-01" "2013-01-01" "2014-01-01" "2015-01-01"

#for year
a <- c(2011:2015)
[1] 2011 2012 2013 2014 2015
df[paste0("y_", a)] <- lapply(a, function(x) if_else(between(a, 
year(df$date1), year(df$date2)), 1, 0, as.numeric(NA)))
library(lubridate)
library(tidyr)
library(dplyr)
df <- data.frame(date1 = c("2011-09-18", "2013-03-06", "2013-08-08"),
                 date2 = c("2012-02-18", "2014-03-06", "2015-02-03"))
df$date1 <- as.Date(parse_date_time(df$date1, "ymd"))
df$date2 <- as.Date(parse_date_time(df$date2, "ymd"))

#identify the years associated with each row.
df$year<-sapply(1:nrow(df), function(i){
  paste(seq(as.numeric(format(df$date1[i], "%Y")), 
            as.numeric(format(df$date2[i], "%Y"))), collapse = ",")})

#separate and convert to wide format
df %>% separate_rows( year, sep=",") %>% 
  mutate(value=1) %>%
  spread(key=year, value=value, fill=0)

#        date1      date2 2011 2012 2013 2014 2015
# 1 2011-09-18 2012-02-18    1    1    0    0    0
# 2 2013-03-06 2014-03-06    0    0    1    1    0
# 3 2013-08-08 2015-02-03    0    0    1    1    1