简体   繁体   English

如何使用“for循环”找到单个动物的体重减轻率?

[英]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.

相关问题 我怎样才能找出损失&gt;中值损失的样本数量; 以及在 R 中使用层次聚类有多少损失 - How could I find out how many samples with loss > the median loss; and how many with loss using hierarchical clustering in R 我怎么能找到GDP的增长率 - How could I find the growth rate of GDP 如何使用 R 进行循环 - How do I do a loop using R 如何使用R中的&#39;tm&#39;包在语料库中设置TF权重 - How do I set up TF weight of terms in corpus using the ‘tm’ package in R 如何使用最终预测在 Tensorflow 之外复制 TensorFlow 损失值? - How do I replicate TensorFlow loss value, outside of Tensorflow, using the final prediction? 如何使用 for 循环求两行的平均值? - How do I find average of two rows with for loop? 如何用r中的gower距离对变量进行加权 - How do I weight variables with gower distance in r 使用“ binary:logistic”时,XGBoost中单个树的权重计算 - weight calculation of individual tree in XGBoost when using “binary:logistic” 在 R 中,如何从更大的数据集创建单独的时间序列(循环遍历并运行 Mann-Kendall 测试)? - In R, how do I create individual time series (to loop trough and run a Mann-Kendall test) from a larger dataset? 我如何在 rbind 之后和嵌套循环内绘制单个 ggplot - How I draw individual ggplot after rbind and inside the nested loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM