[R] adding date/time stamp
Gabor Grothendieck
ggrothendieck at gmail.com
Wed Dec 31 23:41:06 CET 2008
Try pasting this into R (the first portion is from your post):
Lines <- "LST in mph Deg DegF DegF2 % volts Deg
mph2 w/m2
0509010000 0.00 7.8 216.9 45.1 -999 24.4 -999 -999 10.6 0.2
0509010005 0.00 8.6 206.6 45.1 -999 25.2 -999 -999 11.7 0.2
0509010010 0.00 7.8 199.2 44.9 -999 25.4 -999 -999 12.8 0.2
0509010015 0.00 7.7 197.4 44.8 -999 25.4 -999 -999 10.4 0.2
0509010020 0.00 7.6 203.9 44.8 -999 25.3 -999 -999 10.0 0.2
0509010025 0.00 9.3 200.9 44.9 -999 25.3 -999 -999 11.8 0.2
0509010030 0.00 9.4 200.3 44.7 -999 25.5 -999 -999 12.2 0.2
0509010035 0.00 10.0 199.2 44.6 -999 25.9 -999 -999 13.0 0.2
0509010040 0.00 9.5 201.5 44.5 -999 25.9 -999 -999 13.3 0.2
0509010045 0.00 10.8 200.4 44.5 -999 26.1 -999 -999 13.0 0.2
0509010050 0.00 11.8 198.4 44.5 -999 26.1 -999 -999 13.3 0.2
0509010055 0.00 11.0 197.4 44.5 -999 25.5 -999 -999 13.3 0.2
0509010100 0.00 9.7 202.0 44.6 -999 25.1 -999 -999 13.0 0.2
0509010105 0.00 9.0 215.1 44.7 -999 24.9 -999 -999 12.2 0.2
0509010110 0.00 10.1 223.1 44.6 -999 25.1 -999 -999 13.2 0.2
0509010115 0.00 10.4 231.2 44.5 -999 25.5 -999 -999 12.0 0.2
0509010120 0.00 11.0 237.4 44.2 -999 25.9 -999 -999 11.7 0.2
0509010125 0.00 10.6 241.0 44.2 -999 26.0 -999 -999 11.8 0.2
0509010130 0.00 11.1 242.2 44.1 -999 26.2 -999 -999 12.2 0.2
0509010135 0.00 10.6 240.0 44.0 -999 26.5 -999 -999 11.5 0.2
0509010140 0.00 10.1 241.0 44.0 -999 26.4 -999 -999 11.5 0.2
0509010145 0.00 9.8 243.2 44.0 -999 26.6 -999 -999 10.7 0.2
0509010150 0.00 9.3 240.3 43.9 -999 27.0 -999 -999 10.0 0.2
0509010155 0.00 9.3 239.2 43.8 -999 26.8 -999 -999 10.0 0.2
0509010200 0.00 9.2 240.1 43.8 -999 26.6 -999 -999 9.8 0.2
0509010205 0.00 9.0 240.0 43.8 -999 26.6 -999 -999 9.4 0.2
0509010210 0.00 9.2 245.0 43.9 -999 26.3 -999 -999 9.8 0.2
0509010215 0.00 9.4 253.2 44.1 -999 26.4 -999 -999
10.6 0.2"
Lines2 <- "doy yr mon day hr hgt1 hgt2 hgt3 co21 co22 co23 sig1 sig2
sig3 dif flag
244.02083 2005 09 01 00 2.5 5.8 9.1 -999.99 -999.99 -999.99 -999.99
-999.99 -999.99 -999.99 PRE
244.0625 2005 09 01 01 2.5 5.8 9.1 -999.99 -999.99 -999.99 -999.99
-999.99 -999.99 -999.99 PRE
244.10417 2005 09 01 02 2.5 5.8 9.1 -999.99 -999.99 -999.99 -999.99
-999.99 -999.99 -999.99 PRE
244.14583 2005 09 01 03 2.5 5.8 9.1 -999.99 -999.99 -999.99 -999.99
-999.99 -999.99 -999.99 PRE
244.1875 2005 09 01 04 2.5 5.8 9.1 -999.99 -999.99 -999.99 -999.99
-999.99 -999.99 -999.99 PRE
244.22917 2005 09 01 05 2.5 5.8 9.1 -999.99 -999.99 -999.99 -999.99
-999.99 -999.99 -999.99 PRE
244.27083 2005 09 01 06 2.5 5.8 9.1 -999.99 -999.99 -999.99 -999.99
-999.99 -999.99 -999.99 PRE"
library(zoo)
z <- read.zoo(textConnection(Lines), header = TRUE, na.strings = -999,
format = "%y%m%d%H%M", FUN = as.chron,
colClasses = c("character", rep("numeric", 10)))
DF <- read.table(textConnection(Lines2), header = TRUE)
tt <- with(DF, chron(paste(mon, day, yr, sep = "/"),
paste(hr, 0, 0, sep = ":")))
z2 <- zoo(data.matrix(DF), tt)
# suppose we want all rows of z2 but only matching
# rows in z (omit all= if you want all rows of both)
m <- merge(z, z2, all = c(FALSE, TRUE))
On Wed, Dec 31, 2008 at 5:27 PM, Sherri Heck <sheck at ucar.edu> wrote:
> Hi Gabor-
>
> Your suggestion did work. However, maybe I should explain more what I am
> trying to do. Once I have dataset (z) condensed to an hourly format I will
> compare it with another dataset (lets call it y). Here's a tidbit of
> dataset y:
>
> doy yr mon day hr hgt1 hgt2 hgt3 co21 co22 co23 sig1 sig2 sig3 dif flag
>
> 244.02083 2005 09 01 00 2.5 5.8 9.1 -999.99 -999.99 -999.99 -999.99 -999.99
> -999.99 -999.99 PRE
> 244.0625 2005 09 01 01 2.5 5.8 9.1 -999.99 -999.99 -999.99 -999.99 -999.99
> -999.99 -999.99 PRE
> 244.10417 2005 09 01 02 2.5 5.8 9.1 -999.99 -999.99 -999.99 -999.99 -999.99
> -999.99 -999.99 PRE
> 244.14583 2005 09 01 03 2.5 5.8 9.1 -999.99 -999.99 -999.99 -999.99 -999.99
> -999.99 -999.99 PRE
> 244.1875 2005 09 01 04 2.5 5.8 9.1 -999.99 -999.99 -999.99 -999.99 -999.99
> -999.99 -999.99 PRE
> 244.22917 2005 09 01 05 2.5 5.8 9.1 -999.99 -999.99 -999.99 -999.99 -999.99
> -999.99 -999.99 PRE
> 244.27083 2005 09 01 06 2.5 5.8 9.1 -999.99 -999.99 -999.99 -999.99 -999.99
> -999.99 -999.99 PRE
>
> I need to match up the date/time of the datasets and then invoke a
> conditional statement, such as: if z$mph is >= 12 then keep y$co23 for the
> corresponding time/date stamp. But, when I convert "
>
> (09/01/05 00:00:00) (09/01/05 01:00:00) (09/01/05 02:00:00)"
> 9.27500 10.08333 9.20000
>
> to a matrix to invoke this conditional statement, the time/date stamp
> doesn't appear. Does it have to do with the parentheses?
>
> thanks for your time and help and happy new year:
>
> sherri heck
>
> Gabor Grothendieck wrote:
>>
>> Try this:
>>
>>
>>>
>>> aggregate(z$mph, trunc(time(z), "hour"), mean)
>>>
>>
>> (09/01/05 00:00:00) (09/01/05 01:00:00) (09/01/05 02:00:00)
>> 9.27500 10.08333 9.20000
>>
>> On Tue, Dec 30, 2008 at 6:30 PM, Sherri Heck <sheck at ucar.edu> wrote:
>>
>>>
>>> Dear All-
>>>
>>> I have a dataset that is comprised of the following (LST = yymmddhhMM):
>>>
>>>
>>>
>>> LST in mph Deg DegF DegF2 % volts Deg mph2
>>> w/m2
>>> 0509010000 0.00 7.8 216.9 45.1 -999 24.4 -999 -999 10.6
>>> 0.2
>>> 0509010005 0.00 8.6 206.6 45.1 -999 25.2 -999 -999 11.7
>>> 0.2
>>> 0509010010 0.00 7.8 199.2 44.9 -999 25.4 -999 -999 12.8
>>> 0.2
>>> 0509010015 0.00 7.7 197.4 44.8 -999 25.4 -999 -999 10.4
>>> 0.2
>>> 0509010020 0.00 7.6 203.9 44.8 -999 25.3 -999 -999 10.0
>>> 0.2
>>> 0509010025 0.00 9.3 200.9 44.9 -999 25.3 -999 -999 11.8
>>> 0.2
>>> 0509010030 0.00 9.4 200.3 44.7 -999 25.5 -999 -999 12.2
>>> 0.2
>>> 0509010035 0.00 10.0 199.2 44.6 -999 25.9 -999 -999 13.0
>>> 0.2
>>> 0509010040 0.00 9.5 201.5 44.5 -999 25.9 -999 -999 13.3
>>> 0.2
>>> 0509010045 0.00 10.8 200.4 44.5 -999 26.1 -999 -999 13.0
>>> 0.2
>>> 0509010050 0.00 11.8 198.4 44.5 -999 26.1 -999 -999 13.3
>>> 0.2
>>> 0509010055 0.00 11.0 197.4 44.5 -999 25.5 -999 -999 13.3
>>> 0.2
>>> 0509010100 0.00 9.7 202.0 44.6 -999 25.1 -999 -999 13.0
>>> 0.2
>>> 0509010105 0.00 9.0 215.1 44.7 -999 24.9 -999 -999 12.2
>>> 0.2
>>> 0509010110 0.00 10.1 223.1 44.6 -999 25.1 -999 -999 13.2
>>> 0.2
>>> 0509010115 0.00 10.4 231.2 44.5 -999 25.5 -999 -999 12.0
>>> 0.2
>>> 0509010120 0.00 11.0 237.4 44.2 -999 25.9 -999 -999 11.7
>>> 0.2
>>> 0509010125 0.00 10.6 241.0 44.2 -999 26.0 -999 -999 11.8
>>> 0.2
>>> 0509010130 0.00 11.1 242.2 44.1 -999 26.2 -999 -999 12.2
>>> 0.2
>>> 0509010135 0.00 10.6 240.0 44.0 -999 26.5 -999 -999 11.5
>>> 0.2
>>> 0509010140 0.00 10.1 241.0 44.0 -999 26.4 -999 -999 11.5
>>> 0.2
>>> 0509010145 0.00 9.8 243.2 44.0 -999 26.6 -999 -999 10.7
>>> 0.2
>>> 0509010150 0.00 9.3 240.3 43.9 -999 27.0 -999 -999 10.0
>>> 0.2
>>> 0509010155 0.00 9.3 239.2 43.8 -999 26.8 -999 -999 10.0
>>> 0.2
>>> 0509010200 0.00 9.2 240.1 43.8 -999 26.6 -999 -999 9.8
>>> 0.2
>>> 0509010205 0.00 9.0 240.0 43.8 -999 26.6 -999 -999 9.4
>>> 0.2
>>> 0509010210 0.00 9.2 245.0 43.9 -999 26.3 -999 -999 9.8
>>> 0.2
>>> 0509010215 0.00 9.4 253.2 44.1 -999 26.4 -999 -999 10.6
>>> 0.2
>>>
>>> The data are recorded in 5 minute intervals and I would like to condense
>>> it
>>> into hourly means for "mph". For example, I would like the hourly avg of
>>> mph so that the output would be as follows:
>>>
>>> Year Month Day Hour mph
>>> 2005 1 1 0 12
>>> 2005 1 1 1 7
>>> 2005 1 1 2 11, etc.
>>>
>>>
>>> It seems I am able to get the averages but not output the corresponding
>>> date/time stamp. From looking at previous help questions, I think I need
>>> to
>>> us "ts" and "aggregate". Gabor taught me how to convert the date/time
>>> stamp to an easier to manage format (his help is shown below). This is
>>> what
>>> I have so far.
>>> library(zoo)
>>> library(chron)
>>>
>>> z <- read.zoo("SPL 2005 2008 met data 5 min wout full hdr.txt", header =
>>> TRUE, na.strings = -999,
>>> format = "%y%m%d%H%M", FUN = as.chron,
>>> colClasses = c("character", rep("numeric", 10)))
>>> z.ts <- ts(z, frequency=12) #avging 5 min intervals to get hourly avg.
>>> ww <- matrix(aggregate(z.ts[,2], FUN=mean))
>>>
>>> any thoughts as to how to add the time stamp is greatly welcomed!
>>>
>>> sherri heck
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> 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
mailing list