使用`each`还是`times`来处理R中的向量?

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

Using `each` vs `times` for a vector in R?

问题

  1. 在下面的数据集中,
  2. df_final <- data.frame(month = rep(c(1:12), each =30 * 2),
  3. site = rep(c("1","2","3","4","5","6"), times = 60 * 2),
  4. quad = rep(c(1:5), times = 72 * 2),
  5. seed = rep(c(1:2), times = 360))
  6. .. 每个月(1-12),有6个站点(1-6),有5个象限(1-5 `quad`。因此,一个数据集有12 * 6 * 5 = 360行。在这里,我有两个这样的数据集(360 * 2)。我也可以有1000个这样的数据集。对于每个唯一的数据集,我想分配一个不同的种子。
  7. [![enter image description here][1]][1]
  8. 输出应该如下所示:
  9. month site quad seed
  10. 1 1 1 1
  11. 1 1 2 1
  12. 1 1 3 1
  13. 1 1 4 1
  14. 1 1 5 1
  15. 1 1 1 2
  16. 1 1 2 2
  17. 1 1 3 2
  18. 1 1 4 2
  19. 1 1 5 2
  20. 我尝试使用 `seed = rep(c(1:2), each = 360)`,但那样并不奏效。我该如何得到这个输出?我希望有一种通用的方法,这样我就可以为1000个种子做同样的事情。
  21. [1]: https://i.stack.imgur.com/RkeUb.png
英文:

In the following dataset,

  1. df_final &lt;- data.frame(month = rep(c(1:12), each =30 * 2),
  2. site = rep(c(&quot;1&quot;,&quot;2&quot;,&quot;3&quot;,&quot;4&quot;,&quot;5&quot;,&quot;6&quot;), times = 60 * 2),
  3. quad = rep(c(1:5), times = 72 * 2),
  4. seed = rep(c(1:2), times = 360))

.. every month (1-12), has 6 sites (1-6), with 5 quadrants (1-5) quad. So one dataset has 12 * 6 * 5 = 360 rows. Here, I have two such datasets (360 * 2). I could also have 1000 such datasets. For every unqiue dataset, I want to assign a different seed.

使用`each`还是`times`来处理R中的向量?

output should looks like:

  1. month site quad seed
  2. 1 1 1 1
  3. 1 1 2 1
  4. 1 1 3 1
  5. 1 1 4 1
  6. 1 1 5 1
  7. 1 1 1 2
  8. 1 1 2 2
  9. 1 1 3 2
  10. 1 1 4 2
  11. 1 1 5 2

I tried using seed = rep(c(1:2), each = 360 but that didn't work as well. How can I get this output? I want a general way to do it, so that I can do the same for 1000 seeds.

答案1

得分: 1

You can use cur_group_id:

  1. library(dplyr)
  2. df_final %>%
  3. group_by(month, site) %>%
  4. mutate(seed = cur_group_id()) %>%
  5. ungroup() %>% arrange(month, site)

或者在列排序后使用 consecutive_id

  1. library(dplyr) #1.1.0+
  2. df_final %>%
  3. arrange(month, site) %>%
  4. mutate(seed = consecutive_id(month, site))
英文:

You can use cur_group_id:

  1. library(dplyr)
  2. df_final %&gt;%
  3. group_by(month, site) %&gt;%
  4. mutate(seed = cur_group_id()) %&gt;%
  5. ungroup() %&gt;% arrange(month, site)

Or consecutive_id after arranging the columns:

  1. library(dplyr) #1.1.0+
  2. df_final %&gt;%
  3. arrange(month, site) %&gt;%
  4. mutate(seed = consecutive_id(month, site))

答案2

得分: 1

使用 data.table

  1. library(data.table)
  2. setDT(df_final)[order(month, site), seed := rleid(month, site)]

或者

  1. setDT(df_final)[, seed := .GRP, .(month, site)]
英文:

Using data.table

  1. library(data.table)
  2. setDT(df_final)[order(month, site), seed := rleid(month, site)]

Or do

  1. setDT(df_final)[, seed := .GRP, .(month, site)]

huangapple
  • 本文由 发表于 2023年3月7日 01:33:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75654032.html
匿名

发表评论

匿名网友

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

确定