简体   繁体   English

在 data.table 中查找按 2 个因子分组的两个变量的变化

[英]Find change in two variables, grouped by 2 factors, in data.table

I have plots which were visited at irregular intervals to record biomass of several species.我有不定期访问的地块,以记录几种物种的生物量。 I would like to record the change in each species's biomass, and the duration of the interval, at the beginning of the interval我想在间隔开始时记录每个物种的生物量的变化,以及间隔的持续时间

Sample data are样本数据是

plot <- c(rep(1,4), rep(2,3))
species <- c(rep(c('a','b'), 2), rep('a',3))
year <- c(1,1,3,3,2,5,13)
biom <- c(5,2,8,4,3,9,18)

DT <- data.table(plot=plot, sp=sp,year=year,biom=biom)

The desired output would look like所需的 output 看起来像

elapsed = c(2,2,NA,NA,3,8,NA)
dbiom = c(3,2,NA,NA,6,9,NA)

(eg, change in biomass of species a in plot 1, first survey in year 1 to second survey in year 2, was +3, and the elapsed time was 2 years) (例如,plot 1 中物种 a 的生物量变化,第 1 年第一次调查到第 2 年第二次调查,为 +3,经过时间为 2 年)

I have been using the 'shift' operator in data.table but I cannot get it to work我一直在 data.table 中使用“移位”运算符,但我无法让它工作

setkey(DT, plot, sp , year)
cols = c("year","biom")
anscols = paste("lead", cols, sep="_")

b4 <- b3[ , (anscols) := shift(.SD, 1, NA, type = "lead"), 
     .SDcols=cols, by = c(plot, sp)]

I keep getting 'Error in eval(bysub, x, parent.frame()): object '1' not found'我不断收到“评估错误(bysub,x,parent.frame()):object '1' not found”

Would something like this do the trick?这样的事情会奏效吗?

library(data.table)

plot <- c(rep(1,4), rep(2,3))
sp <- c(rep(c('a','b'), 2), rep('a',3))
year <- c(1,1,3,3,2,5,13)
biom <- c(5,2,8,4,3,9,18)

DT <- data.table(plot=plot, sp=sp,year=year,biom=biom)

elapsed = c(2,2,NA,NA,3,8,NA)
dbiom = c(3,2,NA,NA,6,9,NA)

DT[
  order(plot, year),
  .(year, biom,
    elapsed = year - shift(year),
    dbiom = biom - shift(biom)
  ),
  by = c("sp", "plot")
  ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM