0

The data frame is:

head(HSC)

       Date    Y1      M1  Location Year Quarter Month
2010-01-04   0.115   0.245 Chicago 2010      Q1    01
2010-01-05   0.111   0.210 Chicago 2010      Q1    01
2010-01-06   0.113   0.185 Chicago 2010      Q1    01

Then I get monthly average for Y1 and M1

HSCnew<-aggregate(cbind(Y1,M1)~Month+Year,data=HSC,mean)

Month Year     Y1       M1
  01 2010 -0.11935294 -0.04017647
  02 2010 -0.09776471 -0.05600000
  03 2010 -0.06785714  0.01271429
  04 2010 -0.07315789 -0.02457895

Both month and year are characters. is there a way to add date to the right, such date=01-01-2010

jkl
  • 67
  • 1
  • 9
  • 3
    you've aggregated it at a month level - how can your new date have a date value - only month and year are possible right? In your first row of the results, the data is agregated for january 2010, date is irrelevant. – vagabond Apr 23 '15 at 15:01
  • so how could i add a column to HSCnew, such that date= 01/01/2010...01/02/2010? – jkl Apr 23 '15 at 15:06

2 Answers2

0

I don't know how the day of the month is relevant but you could do this:

date <- paste(Year, Month, sep = "-")

require(zoo)
as.yearmon(date)
[1] "Jan 2010"
Roland
  • 127,288
  • 10
  • 191
  • 288
vagabond
  • 3,526
  • 5
  • 43
  • 76
0

You can add the Date column as a Date type with this:

HSCnew$Date <- as.Date(paste(HSCnew$Year, HSCnew$Month, '01', sep='/'))

or just

HSCnew$Date <- paste('01', HSCnew$Month, HSCnew$Year, sep='-')

If you want a string in format dd-mm-yyyy.

mucio
  • 7,014
  • 1
  • 21
  • 33