[R] Questions about date/time and truncating

Jonathan Greenberg jgreenberg at arc.nasa.gov
Sat Oct 21 00:43:24 CEST 2006


Thanks!  Worked like a charm!

hourly_met_data$date <-
as.POSIXct(trunc(as.POSIXct(hourly_met_data$date),"day"))

--j


On 10/20/06 3:31 PM, "jim holtman" <jholtman at gmail.com> wrote:

> trunc returns a type of POSIXlt.  You have to apply 'as.POSIXct' to the
> result.
> 
> On 10/20/06, Jonathan Greenberg <jgreenberg at arc.nasa.gov> wrote:
>> 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
>> 
>> ______________________________________________
>> R-help at stat.math.ethz.ch 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.
>> 
> 


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