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.
我正在使用来自海狸的GPS数据处理数据帧,数据帧包含有动物id的列(参见下面的$ id),这是26级的因素。对于每个海狸,我们有几个GPS值 - 数量因动物而异。
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
我现在想要在15分钟的间隔中创建一个单独的列,其中“每次捕获后的时间”,从0分钟开始。对于15分钟的intervall,我试图创建一个序列
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).
现在我不确定如何定义x所以它指的是每个人。我应该使用拆分功能来拆分数据帧吗?我们也有一个日期/时间列,但问题是我们在白天没有GPS点(当动物睡觉时),导致我们想要从TimePostRel计算中排除的断点(我们只想参考“活跃时间“捕获后”。
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
干杯,帕特里夏
2 个解决方案
#1
1
You can do this very quickly in data.table
. I assume your data is called dta
:
您可以在data.table中快速完成此操作。我假设您的数据称为dta:
library(data.table)
setDT(dta) ## change format
dta[, TimePostRel:=seq(from = 0, along = x, by = 15), by=x]
#2
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
:
plyr包也可以完成这项任务。对于具有一列因子的数据框,请使用ddply的transform选项:
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:
请注意,新数据框的行按顺序排列,以匹配列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
#1
1
You can do this very quickly in data.table
. I assume your data is called dta
:
您可以在data.table中快速完成此操作。我假设您的数据称为dta:
library(data.table)
setDT(dta) ## change format
dta[, TimePostRel:=seq(from = 0, along = x, by = 15), by=x]
#2
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
:
plyr包也可以完成这项任务。对于具有一列因子的数据框,请使用ddply的transform选项:
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:
请注意,新数据框的行按顺序排列,以匹配列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