[R] Questions about date/time and truncating

Jonathan Greenberg jgreenberg at arc.nasa.gov
Sat Oct 21 00:05:58 CEST 2006


I'm getting a weird behavior using R 2.5.0 for MacOS X -- I have a csv file
with a "properly formatted" date/time field, e.g. After reading in the csv
to "hourly_met_data", with a "date" field

hourly_met_data$date <- as.POSIXct(hourly_met_data$date)

works exactly as it is supposed to (e.g. Min/max of that field are
calculated correctly).  However, when I perform this command to convert a
date/time to just a date (e.g. I'm removing the hour portion of the
date/time):

hourly_met_data$newdate <- trunc(hourly_met_data$date,"day")

I get:

Error in `$<-.data.frame`(`*tmp*`, "newdate", value = list(sec = 0, min = 0,
: 
    replacement has 9 rows, data has 252020

If I simply set:
newdate <- trunc(hourly_met_data$date,"day")
And then print the data (e.g. "newdate"), the output appears to have the
right number of entries (252,020):

...
[99973] "2006-06-05" "2006-06-05" "2006-06-05" "2006-06-05" "2006-06-05"
"2006-06-05" "2006-06-05" "2006-06-05" "2006-06-05" "2006-06-05"
"2006-06-05" "2006-06-05"
[99985] "2006-06-05" "2006-06-05" "2006-06-05" "2006-06-05" "2006-06-05"
"2006-06-05" "2006-06-05" "2006-06-05" "2006-06-05" "2006-06-05"
"2006-06-06" "2006-06-06"
[99997] "2006-06-06" "2006-06-06" "2006-06-06"
 [ reached getOption("max.print") -- omitted 152021 entries ]]

But if I enter: 

length(newdate)
[1] 9

hourly_met_data$newdate <- newdate gives me the same error as above.

Why is this happening?  The output appears to have > 250k entries, why is
length returning only 9?

--j

-- 
Jonathan A. Greenberg, PhD
NRC Research Associate
NASA Ames Research Center
MS 242-4
Moffett Field, CA 94035-1000
Office: 650-604-5896
Cell: 415-794-5043
AIM: jgrn307
MSN: jgrn307 at hotmail.com



More information about the R-help mailing list