如何根据 r 中的当前向量有效地更改数据框中向量的值?

How do I efficiently change the values of a vector in a dataframe based off of a current vector in r?

我正在尝试根据我的数据中已提供的月份向量创建一个新向量 "seasons"。我正在使用内置的 txhousing 数据集;我已经过滤了数据框以仅包含有关休斯顿市的信息,并将此新数据框称为休斯顿。

我已经设法将十二个月重新分类为四个季节,但是,我这样做的方式效率不高。有人对我如何优化此代码有任何建议吗?每当我尝试提供月份范围时(例如 houston[houston$month==(3:5),] %<>% mutate(seasons = "spring") 我都会收到错误消息“In month = = 3:5 : 较长的对象长度不是较短对象长度的倍数。

感谢您的帮助! -R新手

houston[houston$month==(1),] %<>% mutate(seasons = "winter")
houston[houston$month==(2),] %<>% mutate(seasons = "winter")
houston[houston$month==(3),] %<>% mutate(seasons = "spring")
houston[houston$month==(4),] %<>% mutate(seasons = "spring")
houston[houston$month==(5),] %<>% mutate(seasons = "spring")
houston[houston$month==(6),] %<>% mutate(seasons = "summer")
houston[houston$month==(7),] %<>% mutate(seasons = "summer")
houston[houston$month==(8),] %<>% mutate(seasons = "summer")
houston[houston$month==(9),] %<>% mutate(seasons = "summer")
houston[houston$month==(10),] %<>% mutate(seasons = "fall")
houston[houston$month==(11),] %<>% mutate(seasons = "fall")
houston[houston$month==(12),] %<>% mutate(seasons = "winter")

一个易于阅读和简单的方法是这样的:

houston[houston$month%in%c(12,2,1),] %<>% mutate(seasons = "winter")
houston[houston$month%in%c(3,4,5),] %<>% mutate(seasons = "spring")
houston[houston$month%in%c(6,7,8),] %<>% mutate(seasons = "summer")
houston[houston$month%in%c(9,10,11),] %<>% mutate(seasons = "fall")

dplyr::case_when 为此提供了干净的编码。

library(dplyr)

# Reprex dataframe (always include one in your questions)
houston <- tibble(month = 1:12)

houston %>%
  mutate(seasons = case_when(month %in% c(1:2, 12) ~ "winter",
                             month %in% 3:5        ~ "spring",
                             month %in% 6:9        ~ "summer",
                             month %in% 10:11      ~ "fall"))

# A tibble: 12 x 2
   month seasons
   <int> <chr>  
 1     1 winter 
 2     2 winter 
 3     3 spring 
 4     4 spring 
 5     5 spring 
 6     6 summer 
 7     7 summer 
 8     8 summer 
 9     9 summer 
10    10 fall   
11    11 fall   
12    12 winter