Skip to content
Back to formatted view

Raw Message

Message-ID: <CAN5afy_zM=kPwCoga1dNRdy94=HN63zaZT0J9XuhubAO5wDhUA@mail.gmail.com>
Date: 2017-02-22T19:33:15Z
From: lily li
Subject: converting time format

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]]