R语言 查找以一月为月份的所有行,并更新年列,使其计入上一年的季节

vawmfj5a  于 2022-12-30  发布在  其他
关注(0)|答案(2)|浏览(129)

我有每日时间序列数据。我希望标识数据中与一月份对应的所有行。对于这些行,我希望更新年列,使其向后移动一年。这将允许在上一年的季节而不是当前年份中考虑一月份的行。
这是一个可复制的代码,它生成了类似于我的数据的内容:

library(dplyr)
library(tibble)

# Set the seed for reproducibility
set.seed(123)

# Create a sequence of dates from 2001 to 2005
dates <- seq(as.Date("2001-01-01"), as.Date("2005-12-31"), by = "day")
# Create a tibble with the dates and random numbers for var1 to var4
df <- tibble(year = year(dates), month = month(dates), day = day(dates),
             var1 = runif(length(dates)), var2 = runif(length(dates)),
             var3 = runif(length(dates)), var4 = runif(length(dates)))

df

有什么想法吗?

r7xajy2e

r7xajy2e1#

对于dplyr的使用,您可能会用case_when执行mutate。我添加了一个新的变量来演示,如果您真的需要,只需修改year即可。

library(dplyr)
library(tibble)
library(lubridate)

# Set the seed for reproducibility
set.seed(123)

# Create a sequence of dates from 2001 to 2005
dates <- seq(as.Date("2001-01-01"), as.Date("2005-12-31"), by = "day")
# Create a tibble with the dates and random numbers for var1 to var4
df <- tibble(year = year(dates), month = month(dates), day = day(dates),
             var1 = runif(length(dates)), var2 = runif(length(dates)),
             var3 = runif(length(dates)), var4 = runif(length(dates)))

# add a new grouping variable
df$countyear <- df$year
df <- df %>% mutate(countyear = case_when(.$month == 1 ~ year - 1, .$month != 1 ~ year))   
> head(df)
# A tibble: 6 x 8
   year month   day   var1  var2  var3  var4 countyear
  <dbl> <dbl> <int>  <dbl> <dbl> <dbl> <dbl>     <dbl>
1  2001     1     1 0.576  0.455 0.517 0.857      2000
2  2001     1     2 0.741  0.934 0.381 0.593      2000
3  2001     1     3 0.0914 0.264 0.717 0.907      2000
4  2001     1     4 0.541  0.818 0.981 0.910      2000
5  2001     1     5 0.603  0.118 0.768 0.586      2000
6  2001     1     6 0.222  0.888 0.614 0.716      2000
lf5gs5x2

lf5gs5x22#

您可以将年份变量显示为整数,并过滤一月(月份“01”),从这些日期中减去一年:

library(dplyr)
library(tibble)

# Set the seed for reproducibility
set.seed(123)

# Create a sequence of dates from 2001 to 2005
dates <- seq(as.Date("2001-01-01"), as.Date("2005-12-31"), by = "day")
# Create a tibble with the dates and random numbers for var1 to var4
df <- tibble(year = as.integer(format(dates, format="%Y")), month = format(dates, format="%m"), day = format(dates, format="%d"),
             var1 = runif(length(dates)), var2 = runif(length(dates)),
             var3 = runif(length(dates)), var4 = runif(length(dates)))

df$year[df$month == "01"] <- df$year[df$month == "01"] - 1

相关问题