[R] Subtract days to dates in POSIXct format

Santiago Guallar sguallar at yahoo.com
Mon Apr 30 15:31:01 CEST 2012


Hello,

I'm having problems working with date values in POSIXct format. Here is what I got (eg.lig attached):


x <- read.table("eg.txt", sep = ',', col.names=c("ok","time","secs","lig")) # it gives time as factor
z <- cbind(x,colsplit(x$time, split="\\s", names=c("date", "clock")))
zh<-cbind(z,colsplit(z$clock,split=":",names=c("h","m","s")))
zn <- subset(zh, zh$lig<= 5 & zh$h<8 | zh$h>=22) # nighttime
zn$timepos<-as.POSIXct(zn$time,tz="GMT",format="%d/%m/%y %H:%M:%S")

I want to assign timepos to its “natural” night, that is, the night that goes from sunset of day x to sunrise of day x+1 corresponds to day x:

zn$night<-ifelse(zn$h>=0 & zn$h<9,zn$timepos –“oneday”,zn$timepos)  #doesn’t work

How can I subtract one day (24 hours) to zn$timepos and obtain a POSIXct output?


Thank you for your help,
Santi
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: eg.txt
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120430/9434eb7b/attachment.txt>


More information about the R-help mailing list