我正在尝试计算13000棵树的相对生长率,我们测量了9年。 我想计算以下公式:
因此,时间2的增长记录减去时间1的对数增长 我可以为单个tree-data.frames执行此操作,但是我无法在原始data.frame中创建RGR列 已经使用log函数(cvallog)转换了增长。
我的数据框:
Subset1= data[data$Id== 1|data$Id==2,]
> Subset1
Id Tree replicate cval cvallog
1 1 A_12N_A1_ABBA 2009 0.029224666 -3.532742212
2 2 A_12N_A2_BEAL 2009 0.007306166 -4.919036573
65 1 A_12N_A1_ABBA 2010 0.317308712 -1.147880124
66 2 A_12N_A2_BEAL 2010 0.459960580 -0.776614488
134 1 A_12N_A1_ABBA 2011 0.653251467 -0.425793129
135 2 A_12N_A2_BEAL 2011 1.040621151 0.039817795
198 1 A_12N_A1_ABBA 2012 0.794725631 -0.229758343
199 2 A_12N_A2_BEAL 2012 2.042820623 0.714331511
262 1 A_12N_A1_ABBA 2013 0.735415424 -0.307319736
263 2 A_12N_A2_BEAL 2013 2.189564416 0.783702627
333 1 A_12N_A1_ABBA 2014 0.942966706 -0.058724303
334 2 A_12N_A2_BEAL 2014 3.135083588 1.142655836
397 1 A_12N_A1_ABBA 2015 0.587495498 -0.531886697
398 2 A_12N_A2_BEAL 2015 3.631681108 1.289695656
463 1 A_12N_A1_ABBA 2016 0.995382216 -0.004628479
464 2 A_12N_A2_BEAL 2016 NA NA
527 1 A_12N_A1_ABBA 2017 0.951695307 -0.049510351
528 2 A_12N_A2_BEAL 2017 3.394292628 1.222095382
这是一棵树的代码:
Subset1= data[data$Id== 1,]
###create the t1 and 2 t2 columns
Subset1$t1=Subset1$cvallog # create a t1 column
t2=as.data.frame((Subset1$cvallog)[-1])# remove first observation, crate a t2 column
t2= as.vector (rbind (t2, 0))
Subset1$t2 = t2 #add t2 to main data.frame so to have t1 and t2 on the same row.
replicate_t2=as.data.frame((Subset1$replicate)[-1])# remove first observation, crate a t2 column
replicate_t2= as.vector (rbind (replicate_t2, 0))
Subset1$replicate_t2 = replicate_t2
#calculate RGR Relative growth
Subset1$RGR = ((Subset1$t2)- (Subset1$t1))/ ((Subset1$replicate_t2)-Subset1$replicate) #difference between log values divided by 1 because we have 1 year interval
当我尝试在具有拆分功能的循环中执行此操作时,我无法使用所有树创建最终输出。
uniqTree <- (split(data, data$Tree))
for (i in 1:length(uniqTree)){
temp =data[data$Id== i,]
temp$t1=temp$cvallog #create t1 column
t2=as.data.frame((temp$cvallog)[-1]) #create t2 column
t2= as.vector (rbind (t2, 0))
temp$t2 = t2
replicate_t2=as.data.frame((temp$replicate)[-1])# remove first observation, crate a t2_replicate column
replicate_t2= as.vector (rbind (replicate_t2, 0))
temp$replicate_t2 = replicate_t2
#calculate RGR Relative growth
temp$RGR = NULL
temp$RGR = ((temp$t2)- (temp$t1)) / (temp$replicate_t2-temp$replicate)
print(temp$RGR)
}
谢谢你的帮助
答案 0 :(得分:0)
如果使用dplyr
包
df %>% arrange(Tree,replicate) %>%
group_by(Tree) %>%
mutate(rt = (cvallog-lag(cvallog))/(replicate-lag(replicate)))
其中df
df = read.table(text= " Id Tree replicate cval cvallog
1 1 A_12N_A1_ABBA 2009 0.029224666 -3.532742212
2 2 A_12N_A2_BEAL 2009 0.007306166 -4.919036573
65 1 A_12N_A1_ABBA 2010 0.317308712 -1.147880124
66 2 A_12N_A2_BEAL 2010 0.459960580 -0.776614488
134 1 A_12N_A1_ABBA 2011 0.653251467 -0.425793129
135 2 A_12N_A2_BEAL 2011 1.040621151 0.039817795
198 1 A_12N_A1_ABBA 2012 0.794725631 -0.229758343
199 2 A_12N_A2_BEAL 2012 2.042820623 0.714331511
262 1 A_12N_A1_ABBA 2013 0.735415424 -0.307319736
263 2 A_12N_A2_BEAL 2013 2.189564416 0.783702627
333 1 A_12N_A1_ABBA 2014 0.942966706 -0.058724303
334 2 A_12N_A2_BEAL 2014 3.135083588 1.142655836
397 1 A_12N_A1_ABBA 2015 0.587495498 -0.531886697
398 2 A_12N_A2_BEAL 2015 3.631681108 1.289695656
463 1 A_12N_A1_ABBA 2016 0.995382216 -0.004628479
464 2 A_12N_A2_BEAL 2016 NA NA
527 1 A_12N_A1_ABBA 2017 0.951695307 -0.049510351
528 2 A_12N_A2_BEAL 2017 3.394292628 1.222095382" )