[R] Subtract days to dates in POSIXct format
Jeff Newmiller
jdnewmil at dcn.davis.CA.us
Tue May 1 01:14:19 CEST 2012
The sunrise and sunset are calculated for each time value on the input vector, but all times are treated in the TZ timezone. I can see that the Canary Islands are in the GMT timezone, so the example TZ is right.
I can see that you entered the latitude and longitude backward (longitude is "x"), but I am not able to look at your data at the moment so I don't know whether that fixes your problem. Maptools assumes one location Depending on where the bird went in the South Atlantic, the assumed latitude could be several hours wrong, though. Also, there is dependence of length of day on latitude (made more noticeable by crossing the equator).
---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.
Santiago Guallar <sguallar at yahoo.com> wrote:
>Thanks a lot, Jeff
>
>Yes, I applied colsplit in package reshape. Sorry, I forgot to state it
>in the piece of code I attached.
>Actually, these are archival data from a geolocator attached to a
>seabird. The origin lies on Gran Canaria and data were gathered all
>along its migratory journey for one full year, from the Canary Islands
>all the way to the South Atlantic Ocean and back.
>Variable lig, which stands for light, indicates daylight (aprox lig>2)
>or its absence (lig<2), so I know when it is nighttime or daytime for
>sure. However, I'd like to compare it with the output of the maptools
>functions.
>If I understood well maptools functions use astronomical algorithms to
>calculate sunrise and sunset based on one location and one POSIXct
>time. If I changed your invented coordinates and time for the real ones
>in my dataset would do the job correctly? When I put the aproximate
>coordinates it yields an incorrect output:
>site <- SpatialPoints( matrix(c(27.82, -15.77),
>nrow=1),proj4string=CRS("+proj=longlat +datum=WGS84")) # didn't touch
>the rest of your code
>
>But I'm probably doing something wrong.
>
>Santi
>
>
>
>
>>________________________________
>> From: Jeff Newmiller <jdnewmil at dcn.davis.ca.us>
>>To: Santiago Guallar <sguallar at yahoo.com>
>>Cc: "r-help at r-project.org" <r-help at r-project.org>
>>Sent: Monday, April 30, 2012 5:51 PM
>>Subject: Re: [R] Subtract days to dates in POSIXct format
>>
>>On Mon, 30 Apr 2012, Santiago Guallar wrote:
>>
>>> Hello,
>>>
>>> I'm having problems working with date values in POSIXct format.
>>
>>Indeed you are.
>>
>>> 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")))
>>
>>colsplit is not defined in base R, and it is redefined in a couple of
>packages. I am guessing you mean the one that is in the reshape
>package.
>>
>>> 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:
>>
>>You are working here with GMT, but there are many other possible
>timezones that this data could correspond to, and if the input time
>really is in GMT, then the interval of night could be anytime.
>>
>>>
>>> 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?
>>
>>Well, a literal answer to your question is:
>>
>>zn$timepos - as.difftime( 1, units="days" )
>>
>>but in response to your stated goal perhaps you should study the
>following:
>>
>>library(maptools)
>>Sys.setenv( TZ="GMT" ) # set to the local timezone for column "time"
>>dta <- read.table( "eg.txt"
>> , sep=','
>> , col.names=c("ok","time","secs","lig")
>> , stringsAsFactors=FALSE)
>>dta$dtm <- as.POSIXct( dta$time, format="%d/%m/%y %T" ) # depends on
>TZ
>># site is invented in GMT timezone for illustration
>>site <- SpatialPoints( matrix( 0, 51.5, nrow=1 )
>> , proj4string=CRS("+proj=longlat +datum=WGS84" )
>)
>>dta$rise <- sunriset( site
>> , dta$dtm
>> , direction="sunrise"
>> , POSIXct.out=TRUE )$time
>>dta$set <- sunriset( site
>> , dta$dtm
>> , direction="sunset"
>> , POSIXct.out=TRUE )$time
>>dta$isnight <- with( dta, !( rise < dtm & dtm < set ) )
>>
>>
>>---------------------------------------------------------------------------
>>Jeff Newmiller The ..... ..... Go
>Live...
>>DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live
>Go...
>> Live: OO#.. Dead: OO#..
>Playing
>>Research Engineer (Solar/Batteries O.O#. #.O#. with
>>/Software/Embedded Controllers) .OO#. .OO#.
>rocks...1k
>>---------------------------------------------------------------------------
>>
>>
More information about the R-help
mailing list