[R] apply a function at: dateX, dateX+1, dateX+2, ....
Tagmarie
Ramgad82 at gmx.net
Fri Dec 7 15:22:52 CET 2012
Dear knowing people,
Dennis Murphy helped me a lot with my first loop last week. Thanks again - I
could have made more than 10 "Thank-You cakes" in the time it saved me!
But now I want to complicate the thing. My ideas didn't work. Let's see if
anyone is smarter ;-)
The following packages are needed:
library(adehabitatHR)
library(rgdal)
library(plyr)
# My dataframe looks somewhat like this:
myframe <- data.frame (Timestamp=c("24.09.2012 09:00", "24.09.2012 10:00",
"24.09.2012 11:00",
"25.09.2012 09:00", "25.09.2012 10:00",
"25.09.2012 11:00",
"26.09.2012 09:00", "26.09.2012 10:00",
"26.09.2012 11:00"),
Longitude=c(8.91617, 8.92700, 8.92711, 8.92722,
8.92733, 8.92744,8.92800, 8.92900, 8.93000 ),
Latitude=c(54.5485, 54.5410, 54.5412, 54.5413,
54.5414, 54.5424,54.5500, 54.5600, 54.5700 ) ,
AnimalID= c(rep("Ernie")))
head(myframe)
myframestime <- as.POSIXct (strptime(as.character(myframe$Timestamp),
"%d.%m.%Y %H:%M"), tz="GMT")
myframe2 <- cbind (myframe,myframestime)
myframe2$Timestamp <- NULL
myframesxy <- project(cbind(myframe2$Longitude,myframe2$Latitude),"+proj=utm
+zone=32 +ellps=WGS84")
colnames(myframesxy) <- c("Long", "Lat")
myframe3 <- cbind(myframe2, myframesxy)
myframe3$Longitude <- NULL
myframe3$Latitude <- NULL
myframe3
str(myframe3)
# and this is what I did with the help of Dennis Murphy last week:
myframe3$day <- as.Date(myframe3$myframestime)
hrfun <- function(d)
{
dxy <- d[, c("Long", "Lat")]
d_ltraj <- as.ltraj(dxy, d$myframestime, id = d$AnimalID)
dLiker <- liker (d_ltraj, sig2 = 18, rangesig1= c(1, 100) )
dBB <- kernelbb(d_ltraj, sig1=dLiker$Ernie$sig1, sig2=18)
data.frame(AnimalID = unique(d$AnimalID), day = unique(d$day), h =
kernel.area(dBB, unout=c("km2")))
}
ddply(myframe3, .(day), hrfun)
# i.e. I calculated the size of the homerange for Animal "Ernie" at every
single day. (The several numbers per day
# represent the several Homerange levels).
# What I want to do now is to calculate the homerange for Animal "Ernie" for
two consecutive days.
# I.e. I want a home range for the timespan 24. to 25. Septemberand the
# home range for the timespan 25. to 26. September. Each day is included
several times in the calculations.
# It would be easy to do for the example above by hand (subset) but not for
my dataset including a whole year.
# does anyone know how to do that?
# It would be very helpful!
Tagmarie
--
View this message in context: http://r.789695.n4.nabble.com/apply-a-function-at-dateX-dateX-1-dateX-2-tp4652488.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list