简体   繁体   English

按季节分组按冬季而不是年份分组

[英]grouping months by winter season instead of year in R

I have got the following data frame 我有以下数据框

year <- c(1949, 1950, 1950, 1950, 1951, 1951, 1951, 1952, 1952, 1952, 1953, 1953, 1953)
month <- c(12, 1, 2, 12, 1, 2, 12, 1, 2, 12, 1, 2, 12)
df <- data.frame(year, month)
 df
   year month
1  1949    12
2  1950     1
3  1950     2
4  1950    12
5  1951     1
6  1951     2
7  1951    12
8  1952     1
9  1952     2
10 1952    12
11 1953     1
12 1953     2
13 1953    12

where month 1 is January and month 12 is December. 其中第1个月是1月,第12个月是12月。 now I would like to group them by winter season. 现在我想在冬季将它们分组。 this would mean that for example month 12 from year 1949 would be grouped with month 1 and 2 from 1950 because they are part of 1 winter season. 这意味着例如1949年的第12个月将与1950年的第1个月和第2个月分组,因为它们是1个冬季的一部分。 the ideal outcome would be: 理想的结果是:

 year month winterseason
1  1949    12            1
2  1950     1            1
3  1950     2            1
4  1950    12            2
5  1951     1            2
6  1951     2            2
7  1951    12            3
8  1952     1            3
9  1952     2            3
10 1952    12            4
11 1953     1            4
12 1953     2            4
13 1953    12            5 

any ideas? 有任何想法吗?

If this is already arranged by the month 如果这已经按月安排

df$winterseason <- cumsum(df$month == 12)
df$winterseason
#[1] 1 1 1 2 2 2 3 3 3 4 4 4 5

This would label each season by a yearqtr class object giving the year and quarter of the last month of each winter. 这将在每个季节标记一个yearqtr类对象,给出每个冬季的最后一个月的年份和季度。 We convert the year and month to a "yearmon" class object and add 1/12 which pushes each month to the next month. 我们将年份和月份转换为"yearmon"类对象,并添加1/12,将每个月推送到下个月。 Then convert that to a "yearqtr" class object. 然后将其转换为"yearqtr"类对象。

library(zoo)

transform(df, season = as.yearqtr(as.yearmon(paste(year, month, sep = "-")) + 1/12))

giving: 赠送:

   year month  season
1  1949    12 1950 Q1
2  1950     1 1950 Q1
3  1950     2 1950 Q1
4  1950    12 1951 Q1
5  1951     1 1951 Q1
6  1951     2 1951 Q1
7  1951    12 1952 Q1
8  1952     1 1952 Q1
9  1952     2 1952 Q1
10 1952    12 1953 Q1
11 1953     1 1953 Q1
12 1953     2 1953 Q1
13 1953    12 1954 Q1

Note that if season is a variable containing the season column values then as.integer(season) and cycle(season) can be used to extract the year and quarter numbers so, for example, if there were also non-winter rows then cycle(season) == 1 , would identify those in the winter. 请注意,如果season是包含season列值的变量,那么as.integer(season)cycle(season)可用于提取年份和季度数,因此,例如,如果还有非冬季行,则cycle(season) == 1 ,会识别冬天的那些。

Try 尝试

year <- c(1949, 1950, 1950, 1950, 1951, 1951, 1951, 1952, 1952, 1952, 1953, 1953, 1953)
month <- c(12, 1, 2, 12, 1, 2, 12, 1, 2, 12, 1, 2, 12)
df <- data.frame(year, month)
df$season <- ifelse(month == 12,year+1,year) - min(year)

This is not very elegant, but produces your ideal outcome 这不是很优雅,但会产生理想的结果

   year month season
1  1949    12      1
2  1950     1      1
3  1950     2      1
4  1950    12      2
5  1951     1      2
6  1951     2      2
7  1951    12      3
8  1952     1      3
9  1952     2      3
10 1952    12      4
11 1953     1      4
12 1953     2      4
13 1953    12      5

Here is an alternative: using magrittr and data.table 这是另一种选择:使用magrittrdata.table

df$winterYear <- ifelse(month %in% c(11,12),year+1,year) %>% data.table::rleidv()

result: 结果:

   year month winterYear
1  1949    12          1
2  1950     1          1
3  1950     2          1
4  1950    12          2
5  1951     1          2
6  1951     2          2
7  1951    12          3
8  1952     1          3
9  1952     2          3
10 1952    12          4
11 1953     1          4
12 1953     2          4
13 1953    12          5

Side note: To be save you can/should sort your data by year,month . 附注:要保存,您可以/应按year,month对数据进行排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM