使用R中的mutate函数转换季节中的数字月份

时间:2019-01-29 21:47:02

标签: r

这里有点令人尴尬的新人问题。

如何将列出月份的数据转换为季节。

winter = c(12, 1, 2), 
spring = c(3, 4, 5)
summer = c(6, 7, 8)
autumn = c(9, 10, 11)
movies.lm <- mutate(movies.lm, thtr_season = if(thtr_rel_month %in% "winter",  "winter"), (thtr_rel_month %in% "spring", "spring"), (thtr_rel_month %in% "summer", "summer"), (thtr_rel_month %in% "autumn", "autumn)

这自然会返回一堆令人愉快的错误。

有人知道我该怎么做吗?

4 个答案:

答案 0 :(得分:0)

您可以使用ifelse的自然结构:

months <- c(1,1,1,2,3,4,5,6,7,8,8,9,11,12)
movies.lm <- data.frame(months)
movies.lm$season <- ifelse(months==12 | months==1 | months==2, "Winter", 
                           ifelse(months==3 | months==4 | months==5, "Spring", 
                                  ifelse(months==6 | months==7 | months==8, "Summer",
                                         ifelse(months==9 | months==10 | months==11, "Autumn", 
                                                NA)))) 
>movies.lm
   months season
1       1 Winter
2       1 Winter
3       1 Winter
4       2 Winter
5       3 Spring
6       4 Spring
7       5 Spring
8       6 Summer
9       7 Summer
10      8 Summer
11      8 Summer
12      9 Autumn
13     11 Autumn
14     12 Winter

mutant在某些情况下可能更优雅,尤其是在处理大数据时,但是在简单情况下,我更喜欢简单的解决方案。

答案 1 :(得分:0)

由于季节性月份是连续的,这对于cut函数来说是一项好工作:

months <- c(1,1,1,2,3,4,5,6,7,8,8,9,11,12)

seasons <- c("Winter", "Spring", "Summer", "Autumn")
indx <- cut(months %% 12, # send 12 to 0
            seq(0, 12, 3), # cut into three month cycles
            labels = F, # return index
            include.lowest = T, # include lower bounds (0, 3, 6, 9)
            right = F # exclude upper bounds (3, 6, 9, 12)
)

seasons[indx]

# [1] "Winter" "Winter" "Winter" "Winter" "Spring" "Spring" "Spring" "Summer"
# [9] "Summer" "Summer" "Summer" "Autumn" "Autumn" "Winter"

cutlabels = F产生一个索引,该索引使您可以对season字符向量进行子集化。

答案 2 :(得分:0)

library(dplyr)

seasons <- c("Winter", "Spring", "Summer", "Autumn")

movies.lm <- data.frame(thtr_rel_month=c(1,7,5,2,12,11,6,3,8,4,5,10,9))

movies.lm <- mutate(movies.lm, thtr_season = seasons[cut((thtr_rel_month%%12)+1, breaks=4)])

movies.lm
#>    thtr_rel_month thtr_season
#> 1               1      Winter
#> 2               7      Summer
#> 3               5      Spring
#> 4               2      Winter
#> 5              12      Winter
#> 6              11      Autumn
#> 7               6      Summer
#> 8               3      Spring
#> 9               8      Summer
#> 10              4      Spring
#> 11              5      Spring
#> 12             10      Autumn
#> 13              9      Autumn

reprex package(v0.2.1)于2019-01-30创建

答案 3 :(得分:0)

使用 dplyr 的 mutate 的替代方法:

> movies.lm <- data.frame(months = c(1,1,1,2,3,4,5,6,7,8,8,9,11,12))
> 
> library(dplyr)
> movies.lm <- movies.lm%>%
+   mutate(season = setNames(rep(c('Summer', 'Autumn', 'Winter','Spring'),each=3),
+                              c(12,1:11))[as.character(months)])
> 
> movies.lm
   months season
1       1 Summer
2       1 Summer
3       1 Summer
4       2 Summer
5       3 Autumn
6       4 Autumn
7       5 Autumn
8       6 Winter
9       7 Winter
10      8 Winter
11      8 Winter
12      9 Spring
13     11 Spring
14     12 Summer