收到错误提示,尝试使用 “for” 循环时出现错误:参数暗示不同数量的行。

huangapple go评论93阅读模式
英文:

Receiving error when trying "for" loop: arguments imply differing number of rows

问题

I've translated the code for you. Here's the translated code:

  1. 我试图找出三个时期内物种丰富度的变化率(即 abundance.period2 / abundance.period1 * 100)。
  2. 我的数据框看起来像这样:

1 时期 物种 丰富度
2 1 A 10
3 2 A 2
4 3 A 4
5 1 B 5
6 2 B 20
7 3 B 0
8 1 C 1

  1. 我需要使用循环,因为我有太多的物种要处理:
  2. ```R
  3. value.final <- data.frame() # 空数据框用于存储循环结果
  4. reps <- length(unique(df$species)) # 每个物种的重复次数
  5. for(i in 1:reps) {
  6. # 按物种子集
  7. subs <- df[df$species == i, ]
  8. subs$species <- factor(subs$species)
  9. # 时期1
  10. per.1 <- subs[subs$period == "1",]
  11. # 时期2
  12. per.2 <- subs[subs$period == "2",]
  13. # 时期3
  14. per.3 <- subs[subs$period == "3",]
  15. # 值计算
  16. per2.per1 <- per.2$abundance / per.1$abundance * 100
  17. per3.per2 <- per.3$abundance / per.2$abundance * 100
  18. # 保存到数据框
  19. values <- data.frame(species = rep(subs$species, length.out = 2),
  20. period = c("per2.per1", "per3.per2"),
  21. value = c(per2.per1, per3.per2))
  22. # 绑定以保存
  23. value.final <- rbind(values, value.final)
  24. }

我不断收到以下错误:
Error in data.frame(species = rep(subs$species, length.out = 2), period = c("per2.per1", : arguments imply differing number of rows: 2, 0

然而,当我尝试逐个物种地进行操作而不使用循环时,一切正常:

  1. subs <- df[df$species == "A", ]
  2. subs$species <- factor(subs$species)
  3. per.1 <- subs[subs$period == "1",]
  4. per.2 <- subs[subs$period == "2",]
  5. per.3 <- subs[subs$period == "3",]
  6. per2.per1 <- per.2$abundance / per.1$abundance * 100
  7. per3.per2 <- per.3$abundance / per.2$abundance * 100
  8. data.frame(species = rep(subs$species, length.out = 2),
  9. period = c("per2.per1", "per3.per2"),
  10. value = c(per2.per1, per3.per2))

我不知道如何修复这个错误,因为我不明白为什么会出现这个错误。非常感谢任何帮助。

  1. Please note that I've translated the code, but the issue you're facing is related to the R programming logic, and it would require debugging in the original code. If you have any questions or need further assistance with understanding the error, feel free to ask.
  2. <details>
  3. <summary>英文:</summary>
  4. I am trying to find the rate of change in species abundance over 3 periods (i.e. abundance.period2 / abundance.period1 * 100).
  5. My dataframe looks like this:

1 Period species Abundance
2 1 A 10
3 2 A 2
4 3 A 4
5 1 B 5
6 2 B 20
7 3 B 0
8 1 C 1

  1. I need to use a loop because I have too many species to do each individually:

value.final <- data.frame() # Empty data frame to fill with loop results

reps <- length(unique(df$species)) # repetition per species

for(i in 1:reps) {

Subset by species

subs <- df[df$species == i, ]
subs$species <- factor(subs$species)

Period 1

per.1 <- subs[subs$period == "1",]

Period 2

per.2 <- subs[subs$period == "2",]

Period 3

per.3 <- subs[subs$period == "3",]

Value calculation

per2.per1 <- per2$abundance/per1$abundance * 100
per3.per2 <- per3$abundance/per2$abundance * 100

Save in dataframe

values <- data.frame(species = rep(subs$species, length.out = 2),
period = c("per2.per1", "per3.per2"),
value = c(per2.per1, per3.per2))

Bind to save

value.final <- rbind(values, value.final)
}

  1. I keep receiving the folloing error:
  2. **Error in data.frame(species = rep(subs$species, length.out = 2), period = c(&quot;per2.per1&quot;, : arguments imply differing number of rows: 2, 0**
  3. However, when I try doing it species by species without using the loop, it works fine:

subs <- df[df$species == "A", ]
subs$species <- factor(subs$species)

per.1 <- subs[subs$period == "1",]
per.2 <- subs[subs$period == "2",]
per.3 <- subs[subs$period == "3",]

per2.per1 <- per2$abundance/per1$abundance * 100
per3.per2 <- per3$abundance/per2$abundance * 100

data.frame(species = rep(subs$species, length.out = 2),
period = c("per2.per1", "per3.per2"),
value = c(per2.per1, per3.per2))

  1. I don&#39;t know how to fix the error because I don&#39;t understand why I am receiving it. Any help is greatly appreciated.
  2. </details>
  3. # 答案1
  4. **得分**: 1
  5. 这是一种可能的方法来解决这个问题...
  6. ```R
  7. # 创建你提供的数据框
  8. df <- data.frame(Period = c(1, 2, 3, 1, 2, 3, 1),
  9. Species = c(rep("A", 3), rep("B", 3), rep("C", 1)),
  10. Abundance = c(10, 2, 4, 5, 20, 0, 1))
  11. # 按照物种拆分成列表元素
  12. specList <- split(df, df$Species)
  13. # 使用sapply返回差异的列表
  14. result <- sapply(specList,
  15. function(x) {
  16. 100*(exp(diff(log(x$Abundance))) - 1)
  17. })
  18. # 将结果重新组合成数据框并打印出来
  19. print(as.data.frame(result[sapply(result, length) == 2]))

我假设你想计算每个期间之间的百分增长(类似于:100*(a1/a0 - 1) ),我使用了diff()函数以及log()和exp()来完成除法。你可以使用你感觉最舒服的方法。

希望这能帮助到你。

英文:

Here's a way you could approach this problem...

  1. # build the data.frame you presented
  2. df &lt;- data.frame(Period = c(1, 2, 3, 1, 2, 3, 1),
  3. Species = c(rep(&quot;A&quot;, 3), rep(&quot;B&quot;, 3), rep(&quot;C&quot;, 1)),
  4. Abundance = c(10, 2, 4, 5, 20, 0, 1))
  5. # split it by Species into list elements
  6. specList &lt;- split(df, df$Species)
  7. # use sapply to return a list of the differences
  8. result &lt;- sapply(specList,
  9. function(x) {
  10. 100*(exp(diff(log(x$Abundance))) - 1)
  11. })
  12. # reform the results into a data.frame and print it
  13. print(as.data.frame(result[sapply(result, length) == 2]))

I'm assuming that you intended to calculate the percent increase between each of the Periods (which would be something more like: 100*(a1/a0 - 1) ) and I used the diff() function along with the log() and exp() to accomplish the division. You should use whatever method you feel most comfortable with.

I hope this helps.

答案2

得分: 1

以下是代码部分的翻译:

  1. library(tidyverse)
  2. df <- tibble(Period = c(1, 2, 3, 1, 2, 3, 1),
  3. Species = c(rep("A", 3), rep("B", 3), rep("C", 1)),
  4. Abundance = c(10, 2, 4, 5, 20, 0, 1))
  5. df %>%
  6. group_by(Species) %>%
  7. reframe(per2.per1 = (Abundance[Period == 2]/ Abundance[Period == 1])*100,
  8. per3.per2 = (Abundance[Period == 3]/ Abundance[Period == 2])*100) %>%
  9. pivot_longer(-Species, names_to = "period")
  10. #> # A tibble: 4 x 3
  11. #> Species period value
  12. #> <chr> <chr> <dbl>
  13. #> 1 A per2.per1 20
  14. #> 2 A per3.per2 200
  15. #> 3 B per2.per1 400
  16. #> 4 B per3.per2 0

希望这有帮助。

英文:

What about this? No loop needed.

  1. library(tidyverse)
  2. df &lt;- tibble(Period = c(1, 2, 3, 1, 2, 3, 1),
  3. Species = c(rep(&quot;A&quot;, 3), rep(&quot;B&quot;, 3), rep(&quot;C&quot;, 1)),
  4. Abundance = c(10, 2, 4, 5, 20, 0, 1))
  5. df |&gt;
  6. group_by(Species)|&gt;
  7. reframe(per2.per1 = (Abundance[Period == 2]/ Abundance[Period == 1])*100,
  8. per3.per2 = (Abundance[Period == 3]/ Abundance[Period == 2])*100) |&gt;
  9. pivot_longer(-Species, names_to = &quot;period&quot;)
  10. #&gt; # A tibble: 4 x 3
  11. #&gt; Species period value
  12. #&gt; &lt;chr&gt; &lt;chr&gt; &lt;dbl&gt;
  13. #&gt; 1 A per2.per1 20
  14. #&gt; 2 A per3.per2 200
  15. #&gt; 3 B per2.per1 400
  16. #&gt; 4 B per3.per2 0

huangapple
  • 本文由 发表于 2023年7月18日 08:12:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76708805.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定