r - Separate year from month in date format -


suppose have list containing year , month:

l <- c(200101, 200102 ,200103, 200104) 

i want separate them can have 2 separate lists. here did no luck:

as.date(l,"%y%m",format = "%y") 

this work :

year_list<-as.numeric(substr(as.numeric(l),1,4)) month_list<-as.numeric(substr(as.numeric(l),5,6)) 

example 1:

> l <- c(200101, 200102 ,200103, 200104) > l [1] 200101 200102 200103 200104  > year_list<-as.numeric(substr(as.numeric(l),1,4)) > year_list [1] 2001 2001 2001 2001  > month_list<-as.numeric(substr(as.numeric(l),5,6)) > month_list [1] 1 2 3 4 

example 2:

> l <- c(200701, 200802 ,200903, 201604) > year_list<-as.numeric(substr(as.numeric(l),1,4)) > year_list [1] 2007 2008 2009 2016 > month_list<-as.numeric(substr(as.numeric(l),5,6)) > month_list [1] 1 2 3 4 

Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -