[英]How do I find the rate of weight loss of individual animals using a 'for loop'?
I'm trying to calculate how much water weight was lost by 13 frogs and toads (5 frogs, 8 toads) placed in front of a fan for a total of 45 minutes (don't worry! all toads and frogs are ok!).我正在尝试计算 13 只青蛙和蟾蜍(5 只青蛙,8 只蟾蜍)在风扇前总共 45 分钟失去了多少水分(别担心!所有的蟾蜍和青蛙都可以!) .
I weighed each individual(g) after 9 minutes, so I have a total of 5 measurements.我在 9 分钟后给每个人称重(g),所以我总共有 5 次测量。 I want to know the rate of water lost of each at 9 minutes interval.我想知道每 9 分钟间隔的失水率。
When I run a for loop, I only get a rate for the first measurement.当我运行 for 循环时,我只能得到第一次测量的速率。 How to get a rate for each 9 minute interval for all 13 animals?如何获得所有 13 只动物每 9 分钟间隔的费率?
This is what my code looks like:这是我的代码的样子:
for(i in 1: length(EWL)) {
for(j in 1:13) {
ft <- EWL[which(EWL$Animal == as.character(j)), ]
wr <- (ft[6, "Weight"] - ft[1, "Weight"]) / 45
EWL$WR[EWL$Animal == as.character(j)] <- wr
}
}
For toad 1, this code gave me -.06, which is repeated for all 5 measurements.对于蟾蜍 1,这段代码给了我 -.06,对所有 5 次测量重复。 Same thing for toad 2, I get -.08 for each interval.蟾蜍 2 也是如此,每个间隔我得到 -.08。 The math is correct for the first interval calculation, it's just repeated for all intervals.数学对于第一个间隔计算是正确的,它只是在所有间隔中重复。
My data looks like this:我的数据如下所示:
> EWL
Time Weight Posture Animal Size Species
1 0 204.4 3 1 L toad
2 9 199.6 3 1 L toad
3 18 197.6 4 1 L toad
4 27 196.3 3 1 L toad
5 36 194.5 4 1 L toad
6 45 192.8 4 1 L toad
7 0 30.7 2 2 S toad
8 9 30.1 2 2 S toad
9 18 29.4 3 2 S toad
10 27 29.1 4 2 S toad
11 36 28.8 5 2 S toad
12 45 28.3 5 2 S toad
dput:输入:
structure(list(Time = c(0, 9, 18, 27, 36, 45, 0, 9, 18, 27, 36,
45), Weight = c(204.4, 199.6, 197.6, 196.3, 194.5, 192.8, 30.7,
30.1, 29.4, 29.1, 28.8, 28.3), Posture = c(3, 3, 4, 3, 4, 4,
2, 2, 3, 4, 5, 5), Animal = c(1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
2), Size = c("L", "L", "L", "L", "L", "L", "S", "S", "S", "S",
"S", "S"), Species = c("toad", "toad", "toad", "toad", "toad",
"toad", "toad", "toad", "toad", "toad", "toad", "toad")), class = "data.frame", row.names = c(NA,
-12L))
Your code calculates weight loss rate wr
per minute from a difference between the first measurement at time 0 and last measurement at time 45. This is a single number per animal.代码计算重量损失速率wr
每分钟从在时间0和最后一次测量的第一测量之间的差在时间45.这是每只动物的单个数字。 The last line then inputs that one number to all lines for the animal.最后一行然后将该一个数字输入到该动物的所有行中。
To calculate wr
per minute for all intervals in a for
cycle, use:要计算for
循环中所有间隔的每分钟wr
,请使用:
for(i in 1:13) {
ft <- EWL[which(EWL$Animal == as.character(i)), ]
# difference between subsequent weight divided by interval length
wr <- diff(ft[, "Weight"], lag = 1) / 9
# first value is NA, because toads did not lose weight at time 0
EWL$WR[EWL$Animal == as.character(i)] <- c(NA, wr)
}
However, note that a generalized linear mixed model might be more suitable for the analysis of this data.但是,请注意,广义线性混合模型可能更适合分析此数据。
# assumes different intercept for each animal
fit <- lme4::lmer(Weight ~ Time + (1 | Animal), data = EWL)
# assumes different slope for each animal
fit <- lme4::lmer(Weight ~ Time + (Time | Animal), data = EWL)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.