Home > Mobile >  Converting character variable into date variable with only years in R
Converting character variable into date variable with only years in R

Time:09-25

I have a character variable with n observations per year, like this:

Years <- c("2010","2010","2011", "2011", "2012", "2012")

I would like R to read these characters as actual years, therefore I tried with

dates <- as.Date(Years, format = "%Y")

But the output obtained is this:

[1] "2010-09-24" "2010-09-24" "2011-09-24" "2011-09-24" "2012-09-24" "2012-09-24"

while I would like to keep them only with the year.

Is it possible to obtain a Date variable with only the years?

Thanks

CodePudding user response:

Date class objects require a year, month and day. If you only have a year then you either have to add a month and day or not use Date class.

Also a time series should have one point for each index value. Aggregate the values corresonding to each year using mean, tail1 <- function(x) tail(x, 1) or other aggregation function so that there is only one point per year.

xts does not support just a year as the index but zoo does and if the series is regularly spaced a ts series could be used as well.

library(zoo)

# note that we are assuming a numeric year
DF <- data.frame(year = c(2010, 2010, 2011, 2012, 2012), value = 1:5)
z <- read.zoo(DF, aggregate = mean)
tt <- as.ts(z)

Another possibility is to use yearmon or yearqtr class. This has both a year and month or a year and a quarter but you don't need a day and internally January or Q1 is stored as a number equal to the year.

library(xts)

zm <- read.zoo(DF, FUN = as.yearmon, aggregate = mean)
xm <- as.xts(zm)

zq <- read.zoo(DF, FUN = as.yearqtr, aggregate = mean)
xq <- as.xts(zq)
  • Related