我有一个数据集,它有一个名为month的变量,每个月都作为一个字符。是否有一种方法可以将dplyr组合几个月来创建一个季节变量?我尝试过以下但是收到了错误:
data %>%
mutate(season = ifelse(month[1:3], "Winter", ifelse(month[4:6], "Spring",
ifelse(month[7:9], "Summer",
ifelse(month[10:12], "Fall", NA)))))
有错误:
Error in mutate_impl(.data, dots) : Column `season` must be length 100798 (the number of rows) or one, not 3
我是R的新手,所以非常感谢任何帮助!
答案 0 :(得分:4)
当有多个键/值时,我们可以使用键/ val数据集
进行连接keyval <- data.frame(month = month.abb,
season = rep(c("Winter", "Spring", "Summer", "Fall"), each = 3),
stringsAsFactors = FALSE)
left_join(data, keyval)
答案 1 :(得分:3)
正确的语法应该是
data %>% mutate(season = ifelse(month %in% 10:12, "Fall",
ifelse(month %in% 1:3, "Winter",
ifelse(month %in% 4:6, "Spring",
"Summer"))))
修改:可能是完成工作的更好方法
temp_data %>%
mutate(
season = case_when(
month %in% 10:12 ~ "Fall",
month %in% 1:3 ~ "Winter",
month %in% 4:6 ~ "Spring",
TRUE ~ "Summer"))
答案 2 :(得分:1)
您也可以尝试使用dplyr::recode
或forcats
中的功能。我认为这是最简单的方法:
library(tidyverse)
library(lubridate)
#>
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:base':
#>
#> date
data <- tibble(month = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"))
data %>%
mutate(
season = fct_collapse(
.f = month,
Spring = c("Mar", "Apr", "May"),
Summer = c("Jun", "Jul", "Aug"),
Autumn = c("Sep", "Oct", "Nov"),
Winter = c("Dec", "Jan", "Feb")
)
)
#> # A tibble: 12 x 2
#> month season
#> <chr> <fct>
#> 1 Jan Winter
#> 2 Feb Winter
#> 3 Mar Spring
#> 4 Apr Spring
#> 5 May Spring
#> 6 Jun Summer
#> 7 Jul Summer
#> 8 Aug Summer
#> 9 Sep Autumn
#> 10 Oct Autumn
#> 11 Nov Autumn
#> 12 Dec Winter
由reprex package(v0.2.0)创建于2018-04-06。