Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

define quarter columns with conditions

Tags:

r

I have a dataframe as follow:

ID Mois Year
A   12   2010
B   01   2011
C   04   2010
D   05   2011
E   07   2011
F   11    2010
G   12    2011
H   03    2010
I   01    2012
J   02    2012

I wouls like to add quarter columns as:

quarter1: ( 12(of n-1), 01 of n, 02 of n): means (12 of 2010, 01 of 2011, 02 of 2011)

quarter2:(03 of n , 04 of n, 05 of n)

quarter3: (06 of n, O7 of n, O8of n)

quarter4:( 09of n, 10 of n, 11 of n)

I have tried this code `

data=cbind(data, quarter=ifelse(data$mois==c(12,1,2), "1",
                            ifelse(data$mois==c(3,4,5),"2",
                                   ifelse(data$mois==c(6,7,8),"3", "4"))))

but it does not work and i dont know how to add the condition of the quarter1 as( 12(of n-1), 01 of n, 02 of n): means (12 of 2010, 01 of 2011, 02 of 2011)

or can we replace data$year where data$month == 12 to year + 1, before doing the quarter?

Any help would be much appreciated.

like image 606
BEN Avatar asked Dec 06 '25 04:12

BEN


2 Answers

1) formula We can use this formula to calculate quarters:

transform(data, YearQ = Year + (Mois == 12), Quarter = Mois %% 12 %/% 3 + 1)

giving:

   ID Mois Year YearQ Quarter
1   A   12 2010  2011       1
2   B    1 2011  2011       1
3   C    4 2010  2010       2
4   D    5 2011  2011       2
5   E    7 2011  2011       3
6   F   11 2010  2010       4
7   G   12 2011  2012       1
8   H    3 2010  2010       2
9   I    1 2012  2012       1
10  J    2 2012  2012       1

2) yearqtr Another possibility is to use "yearqtr" class giving the same result:

library(zoo)

transform(data, YearQ = Year + (Mois == 12), Quarter = cycle(as.yearqtr(Year + Mois/12)))

giving same as (1).

2a) Alternately we may just wish to create yearmon and yearqtr columns:

transform(data, ym = as.yearmon(Year + (Mois -1)/12), yq = as.yearqtr(Year + Mois/12))

giving:

   ID Mois Year       ym      yq
1   A   12 2010 Dec 2010 2011 Q1
2   B    1 2011 Jan 2011 2011 Q1
3   C    4 2010 Apr 2010 2010 Q2
4   D    5 2011 May 2011 2011 Q2
5   E    7 2011 Jul 2011 2011 Q3
6   F   11 2010 Nov 2010 2010 Q4
7   G   12 2011 Dec 2011 2012 Q1
8   H    3 2010 Mar 2010 2010 Q2
9   I    1 2012 Jan 2012 2012 Q1
10  J    2 2012 Feb 2012 2012 Q1

3) switch We can use switch like this:

transform(data, YearQ = Year + (Mois == 12), 
                Quarter = sapply(Mois, switch, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 1)))

giving same as (1).

Note

The input data in reproducible form is:

Lines <- "
ID Mois Year
A   12   2010
B   01   2011
C   04   2010
D   05   2011
E   07   2011
F   11    2010
G   12    2011
H   03    2010
I   01    2012
J   02    2012"
data <- read.table(text = Lines, header = TRUE)
like image 124
G. Grothendieck Avatar answered Dec 07 '25 20:12

G. Grothendieck


If you can do with the new column quarter of class factor, then cut will do it.

m <- data$Mois
m[m == 12] <- 0
data$quarter <- cut(m, breaks = c(-1, 2, 5, 8, 11), labels = as.character(1:4))
rm(m)    # tidy up

If you really need or want class character, just coerce it.

data$quarter <- as.character(data$quarter)

DATA.

dput(data)
structure(list(ID = structure(1:10, .Label = c("A", "B", "C", 
"D", "E", "F", "G", "H", "I", "J"), class = "factor"), Mois = c(12L, 
1L, 4L, 5L, 7L, 11L, 12L, 3L, 1L, 2L), Year = c(2010L, 2011L, 
2010L, 2011L, 2011L, 2010L, 2011L, 2010L, 2012L, 2012L)), .Names = c("ID", 
"Mois", "Year"), class = "data.frame", row.names = c(NA, -10L
))
like image 21
Rui Barradas Avatar answered Dec 07 '25 18:12

Rui Barradas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!