【发布时间】:2019-12-10 11:08:30
【问题描述】:
我有一个数据集,它由对鱼的重量、捕获它们的朱利安日期以及它们的名字的观察组成。我正在寻求根据一年中的某一天(朱利安日期)评估这些鱼的平均增长率。我相信最好的方法是用两个字段组成一个 data.frame:“Julian Date”和“Growth”。这个想法是这样的:对于在 1 月 1 日 (1) 观察到体重为 100 的鱼和在 4 月 10 日 (101) 再次观察到体重为 200 的鱼,生长速率将为 100 克/100 天,或 1 克/天。我将在 data.frame 中将其表示为 100 行,其中“Julian 日期”列由 Julian 日期序列(1:100)组成,“Growth”列由平均增长率(1g/天)组成一整天。
我试图编写一个遍历每条鱼的 for 循环,计算平均增长率,然后创建一个列表,其中每个索引包含儒略日期的序列和增长率(重复次数等于儒略日期序列的长度)。然后我会利用这个函数来组成我的 data.frame。
growth_list <- list() # initialize empty list
p <- 1 # initialize increment count
# Looks at every other fish ID beginning at 1 (all even-number observations are the same fish at a later observation)
for (i in seq(1, length(df$FISH_ID), by = 2)){
rate <- (df$growth[i+1]-df$growth[i])/(as.double(df$date[i+1])-as.double(df$date[i]))
growth_list[[p]] <- list(c(seq(as.numeric(df$date[i]),as.numeric(df$date[i+1]))), rep(rate, length(seq(from = as.numeric(df$date[i]), to = as.numeric(df$date[i+1])))))
p <- p+1 # increase to change index of list item in next iteration
}
# Converts list of vectors (the rows which fulfill above criteria) into a data.frame
growth_df <- do.call(rbind, growth_list)
我的预期结果可以在这里说明:https://imgur.com/YXKLkpK
我的实际结果在这里说明:https://imgur.com/Zg4vuVd
如您所见,实际结果似乎是一个 data.frame,其中有两列指定对象的类型以及原始列表项的长度。也就是说,该数据集的第 1 行包含 169 天的观测间隔,因此包含 169 个儒略日期和 169 次重复增长率。p>
【问题讨论】:
标签: r