[R] Alternative to extended recode sintax?

Luca Meyer lucam1968 at gmail.com
Fri Dec 17 17:08:04 CET 2010


Dear R-users,

I have a factor variable within my data frame which I derive week after week from a POSIXct variable using the cut(var,"weeks") command I have found in the chron package. The levels() command gives me:

[1] "2009-03-30 00:00:00" "2009-04-06 00:00:00" "2009-04-13 00:00:00" "2009-04-20 00:00:00" "2009-04-27 00:00:00" "2009-05-04 00:00:00" "2009-05-11 00:00:00" "2009-05-18 00:00:00"
 [9] "2009-05-25 00:00:00" "2009-06-01 00:00:00" "2009-06-08 00:00:00" "2009-06-15 00:00:00" "2009-06-22 00:00:00" "2009-06-29 00:00:00" "2009-07-06 00:00:00" "2009-07-13 00:00:00"
[17] "2009-07-20 00:00:00" "2009-07-27 00:00:00" "2009-08-03 00:00:00" "2009-08-10 00:00:00" "2009-08-17 00:00:00" "2009-08-24 00:00:00" "2009-08-31 00:00:00" "2009-09-07 00:00:00"
[25] "2009-09-14 00:00:00" "2009-09-21 00:00:00" "2009-09-28 00:00:00" "2009-10-05 00:00:00" "2009-10-12 00:00:00" "2009-10-19 00:00:00" "2009-10-25 23:00:00" "2009-11-01 23:00:00"
[33] "2009-11-08 23:00:00" "2009-11-15 23:00:00" "2009-11-22 23:00:00" "2009-11-29 23:00:00" "2009-12-06 23:00:00" "2009-12-13 23:00:00" "2009-12-20 23:00:00" "2009-12-27 23:00:00"
[41] "2010-01-03 23:00:00" "2010-01-10 23:00:00" "2010-01-17 23:00:00" "2010-01-24 23:00:00" "2010-01-31 23:00:00" "2010-02-07 23:00:00" "2010-02-14 23:00:00" "2010-02-21 23:00:00"
[49] "2010-02-28 23:00:00" "2010-03-07 23:00:00" "2010-03-14 23:00:00" "2010-03-21 23:00:00" "2010-03-29 00:00:00" "2010-04-05 00:00:00" "2010-04-12 00:00:00" "2010-04-19 00:00:00"
[57] "2010-04-26 00:00:00" "2010-05-03 00:00:00" "2010-05-10 00:00:00" "2010-05-17 00:00:00" "2010-05-24 00:00:00" "2010-05-31 00:00:00" "2010-06-07 00:00:00" "2010-06-14 00:00:00"
[65] "2010-06-21 00:00:00" "2010-06-28 00:00:00" "2010-07-05 00:00:00" "2010-07-12 00:00:00" "2010-07-19 00:00:00" "2010-07-26 00:00:00" "2010-08-02 00:00:00" "2010-08-09 00:00:00"
[73] "2010-08-16 00:00:00" "2010-08-23 00:00:00" "2010-08-30 00:00:00" "2010-09-06 00:00:00" "2010-09-13 00:00:00" "2010-09-20 00:00:00" "2010-09-27 00:00:00" "2010-10-04 00:00:00"
[81] "2010-10-11 00:00:00" "2010-10-18 00:00:00" "2010-10-25 00:00:00" "2010-10-31 23:00:00" "2010-11-07 23:00:00" "2010-11-14 23:00:00" "2010-11-21 23:00:00" "2010-11-28 23:00:00"
[89] "2010-12-05 23:00:00" "2010-12-12 23:00:00"

Now what I would like is to have more readable labels, such as 2010-W01 for the first week of 2010, 2009-W34 for the 34th week in 2009, etc....is there an easier way to achieve that than having to write out the all recode sintax:

library(car)
dataset$newvar <- recode(dataset$oldvar, "
	c('2009-03-30 00:00:00')='2009-W13';
	c('2009-04-06 00:00:00')='2009-W14';
# etc...
	c('2010-12-05 23:00:00')='2009-W48';
	c('2010-12-12 23:00:00')='2009-W49';
# etc...this part should be updated with time unless I'll find some automatic procedure	
")

Thanks,
Luca


Luca Meyer
www.lucameyer.com
IBM SPSS Statistics release 19.0.0
R version 2.12.1 (2010-12-16)
Mac OS X 10.6.5 (10H574) - kernel Darwin 10.5.0



More information about the R-help mailing list