[R] converting time format

lily li chocold12 at gmail.com
Wed Feb 22 20:33:15 CET 2017


Hi R users,

I have a dataframe, with year, month, day, and other variables. I wanted to
calculated monthly values of the variables. For example, there is one
variable called 'count'. I use the code below to convert daily data to
monthly data.

df.count.mon = aggregate(count ~ year+month, data= df, sum)

The new dataframe has three columns: year, month, and count. Now I want to
add one more column as 'time', which has the format 'yyyy-mm'. I use the
code below but the new column has all NA values. What is the problem and
how to solve it?

df.count.mon$time = as.Date(paste(df.count.mon$year, df.count.mon$month),
'%Y %m')

I had experience to add one more column with the format 'yyyy-mm-dd', which
works, but not with monthly format. Thanks for your help.

	[[alternative HTML version deleted]]



More information about the R-help mailing list