Convert year-month string to three month bins with gaps - how to assign contiguous ascending values?

守給你的承諾、 提交于 2019-12-11 04:15:38

问题


I have used the code below to "bin" a year.month string into three month bins. The problem is that I want each of the bins to have a number that corresponds where the bin occurs chronologically (i.e. first bin =1, second bin=2, etc.). Right now, the first month bin is assigned to the number 4, and I am not sure why. Any help would be highly appreciated!

> head(Master.feed.parts.gn$yr.mo, n=20)
 [1] "2007.10" "2007.10" "2007.10" "2007.11" "2007.11" "2007.11" "2007.11" "2007.12" "2008.01"
[10] "2008.01" "2008.01" "2008.01" "2008.01" "2008.02" "2008.03" "2008.03" "2008.03" "2008.04"
[19] "2008.04" "2008.04"
> 
> yearmonth_to_integer <- function(xx) {
+   yy_mm <- as.integer(unlist(strsplit(xx, '.', fixed=T)))
+   return( (yy_mm[1] - 2006) + (yy_mm[2] %/% 3) )
+ }
> 
> Cluster.GN <- sapply(Master.feed.parts.gn$yr.mo, yearmonth_to_integer)
> Cluster.GN
2007.10 2007.10 2007.10 2007.11 2007.11 2007.11 2007.11 2007.12 2008.01 2008.01 2008.01 
      4       4       4       4       4       4       4       5       2       2       2 
2008.01 2008.01 2008.02 2008.03 2008.03 2008.03 2008.04 2008.04 2008.04 2008.04 2008.05 
      2       2       2       3       3       3       3       3       3       3       3 
2008.05 2008.05 2008.06 2008.10 2008.11 2008.11 2008.12    <NA> 2009.05 2009.05 2009.05 
      3       3       4       5       5       5       6      NA       4       4       4 
2009.06 2009.07 2009.07 2009.07 2009.09 2009.10 2009.11 2010.01 2010.02 2010.02 2010.02 
      5       5       5       5       6       6       6       4       4       4       4 

UPDATE:

I was asked to provide sample input (year) and the desired output (Cluster.GN).I have a year-month string that has varying numbers of observations for each month, and some months don't have any observations. What I want to do is bin each of the three consecutive months that have data, assigning each three month "bin" a number as shown below.

      yr.mo     Cluster.GN
1  2007.10              1
2  2007.10              1
3  2007.10              1
4  2007.10              1
5  2007.10              1
6  2007.11              1
7  2007.11              1
8  2007.11              1
9  2007.11              1
10 2007.12              1
11 2007.12              1
12 2007.12              1
13 2007.12              1
14 2008.10              2
15 2008.10              2
16 2008.10              2
17 2008.10              2
18 2008.12              2
19 2008.12              2
20 2008.12              2
21 2008.12              2
22 2008.12              2

回答1:


1) Convert the strings to zoo's "yearqtr" class and then to integers:

s <- c("2007.10", "2007.10", "2007.10", "2007.11", "2007.11", "2007.11", 
"2007.11", "2007.12", "2008.01", "2008.01", "2008.01", "2008.01", 
"2008.01", "2008.02", "2008.03", "2008.03", "2008.03", "2008.04", 
"2008.04", "2008.04")

library(zoo)
yq <- as.yearqtr(s, "%Y.%m")
as.numeric(factor(yq))
## [1] 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 3 3 3

The last line could alternately be: 4*(yq - yq[1])+1

Note that in the question 2007.12 is classified as in a different quarter than 2007.10 and 2007.11; however, they are all in the same quarter and we assume you did not intend this.

2) Another possibility depending on what you want is:

f <- factor(s)
nlev <- nlevels(f)
levels(f) <- gl(nlev, 3, nlev)
f
## [1] 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 3 3 3
## Levels: 1 2 3

IF there are missing months then this will give a different answer than (1) so it all depends on what you are looking for.



来源:https://stackoverflow.com/questions/33613830/convert-year-month-string-to-three-month-bins-with-gaps-how-to-assign-contiguo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!