[R] converting time format
drjimlemon at gmail.com
Wed Feb 22 22:10:47 CET 2017
Two problems. You have named the month field "mon" and then refer to
it as "month". Second, as the resolution of as.Date is days, it can't
produce a valid date without specifying the day. Thus:
# make each day the first day of the month
'%Y %m %d')
count year mon time
1 22 2014 1 2014-01-01
2 12 2014 2 2014-02-01
You will get values, but I don't think they are the ones you want.
On Thu, Feb 23, 2017 at 6:33 AM, lily li <chocold12 at gmail.com> wrote:
> 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]]
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help