将月份更改为季节

Changing months to seasons

我在 R 中工作。我有一个数据框,其中有采样日期、采样日期的全名月份和水温。下面是我的数据框示例。

   Date           Month       Temperature
   2016-07-01     July        13
   2017-01-08     January     5
   2018-09-19     September   11
   2019-10-24     October     9

我想要做的是使用月份并创建一个新列,其中标记了季节。出于本项目的目的,我将 1 月至 3 月分类为冬季,4 月至 6 月分类为 Spring,7 月至 9 月分类为夏季,10 月至 12 月分类为秋季。

假设在 Date 字段属于 class Date 末尾的注释中可重复显示的输入——如果不是,则按照注释中所示进行转换。下面将展示各种方法。前两个转换 Date 列(这似乎更容易),最后两个转换 Month 列。

1) yearqtrDate 转换为 yearqtr 对象。使用 cycle 将给出季度(1、2、3 或 4),然后我们可以将季度编号索引到 seasons.

library(zoo)

seasons <- c("Winter", "Spring", "Summer", "Fall")
transform(DF, Season = seasons[cycle(as.yearqtr(Date))])
##         Date     Month Temperature Season
## 1 2016-07-01      July          13 Summer
## 2 2017-01-08   January           5 Winter
## 3 2018-09-19 September          11 Summer
## 4 2019-10-24   October           9   Fall

如果只使用四分之一数字就可以了,那么这个一行就足够了。

transform(DF, Season = cycle(as.yearqtr(Date)))

2) quarters 我们也可以只使用基数 R 通过使用 quarters 函数 returns Q1, Q2Q3Q4 作为给定日期的字符串。在这种情况下,我们需要确保 Seasons.Q 使用这些名称以便使用它进行查找。

Seasons.Q <- c(Q1 = "Winter", Q2 = "Spring", Q3 = "Summer", Q4 = "Fall")
transform(DF, Season = Seasons.Q[quarters(Date)])

如果使用 Q1、Q2、Q3、Q4 代替名称是可以的,那么我们可以将其简化为这样一行:

transform(DF, Season = quarters(Date))

3) match 以上使用了日期字段。我们还可以使用 Month 字段作为我们的输入,将其转换为月份编号 1、2、3、...、12,然后将其转换为可以索引到上面定义的 seasons 中的季度编号。

transform(DF, Season = seasons[(match(Month, month.name) - 1) %/% 3 + 1])

如果四分之一的数字而不是季节名称就足够了,那么我们可以将其简化为一行:

transform(DF, Season = (match(DF$Month, month.name) - 1) %/% 3 + 1)

4) 日期格式 我们还可以使用日期格式将月份转换为日期,然后使用 quarters 和查找 Seasons.Q

transform(DF, Season = Seasons.Q[quarters(as.Date(paste(DF$Month, "001"), "%B %y%d"))])

备注

Lines <- "
 Date           Month       Temperature
   2016-07-01     July        13
   2017-01-08     January     5
   2018-09-19     September   11
   2019-10-24     October     9"
DF <- read.table(text = Lines, header = TRUE)
DF$Date <- as.Date(DF$Date)

您可以这样做:

library(tidyverse)
library(lubridate)

df %>% 
  mutate(season = lubridate::quarter(Date)) %>% 
  mutate(season = case_when(
    season == 1 ~ 'Winter',
    season == 2 ~ 'Spring',
    season == 3 ~ 'Summer',
    season == 4 ~ 'Fall'
  ))
#>         Date     Month Temperature season
#> 1 2016-07-01      July          13 Summer
#> 2 2017-01-08   January           5 Winter
#> 3 2018-09-19 September          11 Summer
#> 4 2019-10-24   October           9   Fall

或少一行:

df %>% 
  mutate(season = case_when(
    Month %in% c('January', 'February', 'March') ~ 'Winter',
    Month %in% c('April', 'May', 'June') ~ 'Sring',
    Month %in% c('August', 'September', 'July') ~ 'Summer',
    Month %in% c('October', 'November', 'December') ~ 'Fall'
  ))

这是一个选项,我们创建一个命名向量并使用它来匹配和替换 'Month' 以创建新列

library(dplyr)
nm1 <- setNames(rep(c("Winter", "Spring", "Summer", "Fall"),
        each = 3), month.name)
df1 %>% 
    mutate(Season = nm1[Month])

-输出

#         Date     Month Temperature Season
#1 2016-07-01      July          13 Summer
#2 2017-01-08   January           5 Winter
#3 2018-09-19 September          11 Summer
#4 2019-10-24   October           9   Fall

或者可以在base R

完成
df1$Season = nm1[df1$Month]

数据

df1 <- structure(list(Date = c("2016-07-01", "2017-01-08", "2018-09-19", 
"2019-10-24"), Month = c("July", "January", "September", "October"
), Temperature = c(13L, 5L, 11L, 9L)), class = "data.frame",
row.names = c(NA, 
-4L))