r - Separate year from month in date format -
this question has answer here:
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
Post a Comment