[R] create zoo object within a for loop
Wiebke Ullmann
wiebke.ullmann at uni-potsdam.de
Thu Oct 15 13:07:01 CEST 2015
Dear everyone.
I have a data frame with relocation data of several animals
>head(data)
timestamp
individual
easting
northing
25.03.2014
07:00
animal1
410712.5
5913542
25.03.2014
08:00
animal1
410716.8
5913536
25.03.2014
12:00
animal1
410717.9
5913538
25.03.2014
17:00
animal1
410814.2
5913827
25.03.2014
18:00
animal1
410577.2
5912917
25.03.2014
19:00
animal1
410188.2
5913556
25.03.2014
20:00
animal1
410413.8
5913581
25.03.2014
21:00
animal1
409929.5
5913376
25.03.2014
22:00
animal1
410007.5
5913425
25.03.2014
07:00
animal2
410712.5
5913542
25.03.2014
08:00
animal2
410716.8
5913536
25.03.2014
12:00
animal2
410717.9
5913538
25.03.2014
17:00
animal2
410814.2
5913827
25.03.2014
18:00
animal2
410577.2
5912917
25.03.2014
19:00
animal2
410188.2
5913556
25.03.2014
20:00
animal2
410413.8
5913581
25.03.2014
21:00
animal2
409929.5
5913376
25.03.2014
22:00
animal2
410007.5
5913425
26.03.2014
08:00
animal3
410546.3
5913590
26.03.2014
09:00
animal3
410668.3
5913467
26.03.2014
10:00
animal3
410665.3
5913556
26.03.2014
11:00
animal3
410652.9
5913450
26.03.2014
12:00
animal3
410649.4
5913464
Some hours in the timestamp are missing. I would like to fill the timestamp with the missing hours and the other variables with the last occuring information. It should look like this:
timestamp
individual
easting
northing
25.03.2014
07:00
animal1
410712.5
5913542
25.03.2014
08:00
animal1
410716.8
5913536
25.03.2014
09:00
animal1
410716.8
5913536
25.03.2014
10:00
animal1
410716.8
5913536
25.03.2014
11:00
animal1
410716.8
5913536
25.03.2014
12:00
animal1
410717.9
5913538
...
I know how to do this with a data frame that only includes the information of one animal. I used an index in zoo:
>animal1$time <- strptime(animal1$timestamp, format="%Y-%m-%d %H:%M:%S")
>animal1$time <- as.POSIXct(format(round(animal1$time, units="hours"),
format="%Y-%m-%d %H:%M"))
>animal1.zoo <- zoo(animal1[,-5],animal1[,5]) #set Index
>animal1m <- merge(animal1.zoo,
zoo(,seq(animal1[1,5],animal1[length(animal1$timestamp),5],by="hour")),
all=TRUE)
And filled the other variables with na.locf.
But I do not know how to do this in one go for all the animals. I would like to use a for loop or lappy for lists.
I would be very glad if you could help me out. Thank you very much in advance.
Cheers,
Vivi
[[alternative HTML version deleted]]
More information about the R-help
mailing list