samedi 3 décembre 2016

categorize month `factor` to time periods in data.frame

Say I have a data set like this,

df <- structure(list(yr_month = structure(1:7, .Label = c("2016-1", "2016-2", 
"2016-3", "2016-4", "2016-5", "2016-6", "2016-7"), class = "factor"), 
    a = c(4.14, 2.83, 3.71, 4.15, 4.63, 4.91, 5.31), b = c(4.25, 
    3.5, 3.5, 3.5, 3.5, 3.5, 5)), .Names = c("yrQ", "a", "b"
), row.names = c(NA, 7L), class = "data.frame")
df
#      yrQ    a    b
# 1 2016-1 4.14 4.25
# 2 2016-2 2.83 3.50
# 3 2016-3 3.71 3.50
# 4 2016-4 4.15 3.50
# 5 2016-5 4.63 3.50
# 6 2016-6 4.91 3.50
# 7 2016-7 5.31 5.00

Now, I can use ifelse() to categorize a numeric variable. Like this,

df$a.cat <- ifelse(df$a < 3.8, c("tiny"), ifelse(df$a < 4.8, c("medium"), c("huge")) )
df
#      yrQ    a    b  a.cat
# 1 2016-1 4.14 4.25 medium
# 2 2016-2 2.83 3.50   tiny
# 3 2016-3 3.71 3.50   tiny
# 4 2016-4 4.15 3.50 medium
# 5 2016-5 4.63 3.50 medium
# 6 2016-6 4.91 3.50   huge
# 7 2016-7 5.31 5.00   huge

but, what if I want to crate a variable signifying some time periods. Say before Mar 2016, 2016-3, between 2016-3 and 2016-5, and after 2016-5. I realize I can transform the data to ts and then use window() to cut it up and then put it back together, but isn't there a smarter way to get to something like this using if else on yrQ?

It's something like this I want to get to,

  yr.cat    yrQ    a    b
1    "A" 2016-1 4.14 4.25
2    "A" 2016-2 2.83 3.50
3    "B" 2016-3 3.71 3.50
4    "B" 2016-4 4.15 3.50
5    "B" 2016-5 4.63 3.50
6    "C" 2016-6 4.91 3.50
7    "C" 2016-7 5.31 5.00

Aucun commentaire:

Enregistrer un commentaire