Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/70.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 2:1982-05-02阿留申群岛0.8349334 1983 5 #>3:1982-05-03阿留申群岛-1.3622227 1983 5 #>4:1982-05-04阿留申群岛1.4327587 1983 5 #>5:1982-05-05阿留申群岛0.5068226 1983 5 #> --- #>8048:1999-04-27东白令海-1.29245941999 5 #>8049:1999-04-28东白令海0.7519078 1999 5 #>8050:1999-04-29东白令海-1.0185174 1999 5 #>8051:1999-04-30东白令海-1.4322252 1999 5 #>8052:1999-05-01东白令海-1.0412836 1999 5_R_Date_Time Series_Tidyverse_Lubridate - Fatal编程技术网

R 2:1982-05-02阿留申群岛0.8349334 1983 5 #>3:1982-05-03阿留申群岛-1.3622227 1983 5 #>4:1982-05-04阿留申群岛1.4327587 1983 5 #>5:1982-05-05阿留申群岛0.5068226 1983 5 #> --- #>8048:1999-04-27东白令海-1.29245941999 5 #>8049:1999-04-28东白令海0.7519078 1999 5 #>8050:1999-04-29东白令海-1.0185174 1999 5 #>8051:1999-04-30东白令海-1.4322252 1999 5 #>8052:1999-05-01东白令海-1.0412836 1999 5

R 2:1982-05-02阿留申群岛0.8349334 1983 5 #>3:1982-05-03阿留申群岛-1.3622227 1983 5 #>4:1982-05-04阿留申群岛1.4327587 1983 5 #>5:1982-05-05阿留申群岛0.5068226 1983 5 #> --- #>8048:1999-04-27东白令海-1.29245941999 5 #>8049:1999-04-28东白令海0.7519078 1999 5 #>8050:1999-04-29东白令海-1.0185174 1999 5 #>8051:1999-04-30东白令海-1.4322252 1999 5 #>8052:1999-05-01东白令海-1.0412836 1999 5,r,date,time-series,tidyverse,lubridate,R,Date,Time Series,Tidyverse,Lubridate,由(v2.0.0)于2021-05-20创建的听起来像是要非等联接。这很容易用data.table完成,而且速度非常快。以下是一个轻微修改MWE的示例: 库(data.table) #调查 ai_dat=数据表(年份=c(1983、1986、1991、1994、1997), region=“阿留申群岛”,“startmonth”=5) ebs_dat=数据表(年份=序号(1983、1999、1), region=“东白令海”,“开始月”=5) #绑定并创建日期(和截止日期)变量 surv_dat

由(v2.0.0)于2021-05-20创建的

听起来像是要非等联接。这很容易用data.table完成,而且速度非常快。以下是一个轻微修改MWE的示例:

库(data.table)
#调查
ai_dat=数据表(年份=c(1983、1986、1991、1994、1997),
region=“阿留申群岛”,“startmonth”=5)
ebs_dat=数据表(年份=序号(1983、1999、1),
region=“东白令海”,“开始月”=5)
#绑定并创建日期(和截止日期)变量
surv_dat=rbind(ai_dat,ebs_dat)
surv_dat[,startdate:=as.IDate(粘贴(年份,startmonth,'01',九月='-'))
][,截止日期:=起始日期-365L]
#制造温度df
临时日期=CJ(日期=序号)(如日期(“1982-01-01”)、日期(“1999-12-31”)、“天”),
区域=c(“阿留申群岛”,“东白令海”)
#添加温度变量
温度数据$temp=rnorm(nrow(温度数据))
#创建重复的日期变量(将使加入后处理更容易)
温度数据[,匹配日期:=日期]
#可选:设置键以提高连接性能
设置键(surv_dat,region,startdate)
设置键(温度数据、区域、匹配日期)
#魔法发生的地方:非等连接
surv_dat=温度[surv_dat,on=。(region==region,
匹配日期=截止日期)]
#可选:删除不需要的列
surv_dat[,c('matchdate','matchdate.1'):=NULL][]
#>日期区域温度年份开始月
#>1:1982-05-01阿留申群岛0.3680810 1983 5
#>2:1982-05-02阿留申群岛0.8349334 1983 5
#>3:1982-05-03阿留申群岛-1.3622227 1983 5
#>4:1982-05-04阿留申群岛1.4327587 1983 5
#>5:1982-05-05阿留申群岛0.5068226 1983 5
#>   ---                                                         
#>8048:1999-04-27东白令海-1.29245941999 5
#>8049:1999-04-28东白令海0.7519078 1999 5
#>8050:1999-04-29东白令海-1.0185174 1999 5
#>8051:1999-04-30东白令海-1.4322252 1999 5
#>8052:1999-05-01东白令海-1.0412836 1999 5

由(v2.0.0)

于2021年5月20日创建,输入是包含在脚本I中生成的数据帧。预期的输出是我上面描述的附加列
ref\u match
,但不知道如何生成。输入是我包含的脚本中生成的数据帧。预期的输出是我上面描述的附加列
ref\u match
,但不知道如何生成。
  region           date        year month month_year ref_year                temperature     
  <chr>            <date>     <dbl> <dbl> <chr>      <chr>                         <dbl>
1 aleutian_islands 1982-06-09  1982     6 6-1982     aleutian_islands-5-1983           0   
  region           date        year month month_year ref_year                temperature
  <chr>            <date>     <dbl> <dbl> <chr>      <chr>                         <dbl>
1 aleutian_islands 2013-07-22  2013     7 7-2013     aleutian_islands-5-2014       0.998
# load packages
library(tidyverse)
library(lubridate)
set.seed=10

# make survey dfs
ai_dat <- data.frame("year" = c(1983, 1986, 1991, 1994, 1997), "region" = "aleutian_islands", "startmonth" = 5)
ebs_dat <- data.frame("year" = seq(1983, 1999, 1), "region" = "eastern_bering_sea", "startmonth" = 5)

# join and create what will become ref_year column
surv_dat <- rbind(ai_dat, ebs_dat) %>% 
  mutate(month_year = paste0(startmonth,"-",year)) %>%
  select(region, month_year) %>%
  distinct() %>%
  mutate(region_month_year = paste0(region,"-",month_year))

# expand out to all possible month*year combinations for joining with temperature
surv_dat_exploded <- expand.grid(month=seq(1, 12, 1), year=seq(1982, 2000, 1), region=c('aleutian_islands','eastern_bering_sea')) %>% # get a factorial combo of every possible month*year; have to start in 1982 even though we can't use surveys before 1983 because we need to match to temperature data from 1982
  mutate(region_month_year = paste0(region,"-",month,"-",year)) %>% # create unique identifier
  mutate(ref_year = ifelse(region_month_year %in% surv_dat$region_month_year, region_month_year, NA),
         month_year = paste0(month,"-",year)) %>% 
  select(region, month_year, ref_year) %>% 
  distinct() %>% 
  group_by(region) %>% 
  fill(ref_year, .direction="up") %>%  # fill in each region with the survey to which env data from each month*year should correspond
  ungroup() 

# make temperature dataset and join in survey ref_year column 
temp_dat <- data.frame(expand.grid(date=seq(ymd("1982-01-01"), ymd("1999-12-31"), "days"), region=c('aleutian_islands','eastern_bering_sea'))) %>% 
  mutate(temperature = rnorm(nrow(.), 10, 5),  # fill in with fake data
         year = year(date),
         month = month(date),
         month_year = paste0(month,"-",year)) %>% 
  left_join(surv_dat_exploded, by=c('region','month_year')) %>% 
  filter(!is.na(ref_year))# get rid of dates that are after any ref_year

# load packages
library(tidyverse)
library(lubridate)
set.seed=10

# make survey dfs
ai_dat <- data.frame("year" = c(1983, 1986, 1991, 1994, 1997), "region" = "aleutian_islands", "startmonth" = 5)
ebs_dat <- data.frame("year" = seq(1983, 1999, 1), "region" = "eastern_bering_sea", "startmonth" = 5)

# join and create what will become ref_year column
surv_dat <-
  rbind(ai_dat, ebs_dat) %>% 
  mutate(year_month = paste0(year,"-",startmonth),
         region_year_month = paste0(region,"-",year,"-",startmonth)) 


# expand out to all possible month*year combinations for joining with temperature
surv_dat_exploded <-
  expand.grid(month=seq(01, 12, 1), year=seq(1982, 2000, 1), region=c('aleutian_islands','eastern_bering_sea')) %>% # get a factorial combo of every possible month*year; have to start in 1982 even though we can't use surveys before 1983 because we need to match to temperature data from 1982
  mutate(year_month = paste0(year,"-",month)) %>%
  mutate(region_year_month = paste0(region,"-",year,"-",month)) %>% 
  mutate(ref_year = ifelse(region_year_month %in% surv_dat$region_year_month, region_year_month,NA)) %>%
  group_by(region) %>% 
  fill(ref_year, .direction="up") %>%  # fill in each region with the survey to which env data from each month*year should correspond
  ungroup() 

# make temperature dataset and join in survey ref_year column 
temp_dat <- data.frame(expand.grid(date=seq(ymd("1982-01-01"), ymd("1999-12-31"), "days"), region=c('aleutian_islands','eastern_bering_sea'))) %>% 
  mutate(temperature = rnorm(nrow(.), 10, 5),  # fill in with fake data
         year = year(date),
         month = month(date),
         year_month = paste0(year,"-",month))

final_df <- 
  left_join(temp_dat, surv_dat_exploded, by=c('region','year_month')) %>% 
    #split ref_column in ref_year and ref_region
  separate(ref_year, c("ref_region","ref_year"), "-", extra="merge") %>%
    #convert ref_year into date
    mutate_at("ref_year", as.Date, format= "%Y-%M") %>% 
    #round it down to be in the first day of the month (not needed if the day matters)
    mutate_at("ref_year", floor_date, "month" ) %>% 
    #difference between observed and the reference
    mutate(diff_days = date - ref_year) %>% 
    # ifelse statement for capturing values of interest
    mutate(temp_valid = ifelse(between(diff_days, -365, 0),temperature,NA))