0

I'm working on a dataframe with GPS data from beavers, the dataframe includes on column with the animals id (see $id below) which is a factor with 26 levels. For each beaver, we have several GPS values - the number differs from animal to animal.

I now want to create a separate column with "Time after capture" per individual in 15 min intervalls, starting at 0 min. For the 15 min intervall I tried to create a sequence

TimePostRel <- seq(from = 0, along = x, by = 15)

Now I'm not sure how to define x so it refers to each individual. Should I use the split function to split up the dataframe? We do have a date/time column too, but the problem is that we have no GPS points during daytime (when the animals are sleeping), resulting in breaks that we want to exclude from the TimePostRel calculations (we just want to refer to "active time" after capture).

This is the dataframe:

'data.frame':   6425 obs. of  22 variables:
 $ nb              : int  1 2 3 4 5 6 7 8 9 10 ...
 $ x               : num  517710 517680 NA 517625 517624 ...
 $ y               : num  6587730 6587759 NA 6587929 6588014 ...
 $ date            : POSIXct, format: "2010-04-10 05:15:00" "2010-04-10 05:30:00" "2010-04-10         05:45:00" "2010-04-10 06:00:00" ...
 $ dx              : num  -30.2 NA NA -0.4 -39.2 ...
 $ dy              : num  28.8 NA NA 85.7 126.8 ...
 $ dist            : num  41.7 NA NA 85.7 132.7 ...
 $ dt              : num  900 900 900 900 900 900 900 900 NA 900 ...
 $ R2n             : num  0 1743 NA 46880 88416 ...
 $ abs.angle       : num  2.38 NA NA 1.58 1.87 ...
 $ rel.angle       : num  NA NA NA NA 0.295 ...
 $ id              : Factor w/ 26 levels "Andreas","Apple",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ burst           : Factor w/ 329 levels "Andreas.1","Andreas.2",..: 1 1 1 1 1 1 1 1 1 2 ...
 $ sex             : int  2 2 NA 2 2 2 NA 2 2 2 ...
 $ season          : int  2 2 NA 2 2 2 NA 2 2 2 ...
 $ try             : int  33 34 NA 36 37 38 NA 39 40 41 ...
 $ x.sats          : int  5 5 NA 5 5 5 NA 6 5 6 ...
 $ hdop            : num  2.1 4.2 NA 2.7 3.3 2.1 NA 2.5 2.8 2.2 ...
 $ lodge.x         : num  517595 517595 NA 517595 517595 ...
 $ lodge.y         : num  6587806 6587806 NA 6587806 6587806 ...
 $ NSD_lodge       : num  19039 9440 NA 15909 44268 ...
 $ nsd_1stGPSpoint : num  0 1743 NA 46880 88416 ...

Somebody nows how to solve this? Thanks in advance!!

Cheers, Patricia

ilir
  • 3,236
  • 15
  • 23
Pat
  • 217
  • 1
  • 6

2 Answers2

1

You can do this very quickly in data.table. I assume your data is called dta:

library(data.table)
setDT(dta)   ## change format
dta[, TimePostRel:=seq(from = 0, along = x, by = 15), by=x]
ilir
  • 3,236
  • 15
  • 23
0

The plyr package can also accomplish this task. For a data frame that has a column of factors, use the transform option of ddply:

library(plyr)
# create a data frame where column x is a factor
df <- data.frame(x=c(rep("b",6),rep("a",3),rep("c",4)))
# apply sequence to each level within x
df <- ddply(df,"x",transform,t=seq(from=0,by=15,length.out=length(x)))

Note that the rows of the new data frame are ordered to match the factor levels of column x:

print(df)
   x  t
1  a  0
2  a 15
3  a 30
4  a 45
5  a 60
6  a 75
7  b  0
8  b 15
9  b 30
10 c  0
11 c 15
12 c 30
13 c 45