zoo
Converting year and month (“yyyy-mm” format) to a date?
Since dates correspond to a numeric value and a starting date, you indeed need the day. If you really need your data to be in Date format, you can just fix the day to the first of each month manually by pasting it to the date: month <- “2009-03″ as.Date(paste(month,”-01″,sep=””))
Extract month and year from a zoo::yearmon object
Use the format() method for objects of class “yearmon”. Here is your example date (properly created!) date1 <- as.yearmon(“Mar 2012”, “%b %Y”) Then we can extract the date parts as required: > format(date1, “%b”) ## Month, char, abbreviated [1] “Mar” > format(date1, “%Y”) ## Year with century [1] “2012” > format(date1, “%m”) ## numeric month … Read more
Replacing NAs with latest non-NA value
You probably want to use the na.locf() function from the zoo package to carry the last observation forward to replace your NA values. Here is the beginning of its usage example from the help page: library(zoo) az <- zoo(1:6) bz <- zoo(c(2,NA,1,4,5,2)) na.locf(bz) 1 2 3 4 5 6 2 2 1 4 5 2 … Read more