在一组中随机分配变量的值

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

Randomly assign the value of a variable within a group

问题

我想要在R中为每个id_group中的id_individual随机分配值0或1。每个组由两个个体组成,随机变量应满足以下条件(在一个组内):sum(random_var)==1。

更具体地说,我希望我的随机变量如下所示:

ID_GROUP ID_INDIVIDUAL Random_var
1 1 1
1 2 0
2 1 0
2 2 1

有没有办法在R中编写这个请求的代码?

谢谢!

Chloé

英文:

I would like to randomly assign (in R) for each id_individual within an id_group the value 0 or 1. Each group is composed of two individuals and the random_var should respect the following condition (within a group) : sum(random_var)==1.

More precisely, I would like my random var be like :

ID_GROUP ID_INDIVIDUAL Random_var
1 1 1
1 2 0
2 1 0
2 2 1

Any idea how to code this request in R?

Thanks!

Chloé

答案1

得分: 3

你可以在 ave 内部尝试使用 sample

  1. > transform(df, Random_var = +(ave(ID_INDIVIDUAL, ID_GROUP, FUN = sample) == 1))
  2. ID_GROUP ID_INDIVIDUAL Random_var
  3. 1 1 1 0
  4. 2 1 2 1
  5. 3 2 1 1
  6. 4 2 2 0
英文:

You can try sample within ave

  1. > transform(df, Random_var = +(ave(ID_INDIVIDUAL, ID_GROUP, FUN = sample) == 1))
  2. ID_GROUP ID_INDIVIDUAL Random_var
  3. 1 1 1 0
  4. 2 1 2 1
  5. 3 2 1 1
  6. 4 2 2 0

答案2

得分: 2

  1. 使用`dplyr`中的`group_by`选项如下:
  2. ``` r
  3. library(dplyr)
  4. df %>%
  5. group_by(ID_GROUP) %>%
  6. mutate(Random_var = sample(0:1))
  7. #> # A tibble: 4 × 3
  8. #> # Groups: ID_GROUP [2]
  9. #> ID_GROUP ID_INDIVIDUAL Random_var
  10. #> <int> <int> <int>
  11. #> 1 1 1 0
  12. #> 2 1 2 1
  13. #> 3 2 1 1
  14. #> 4 2 2 0

2023-05-26使用 reprex v2.0.2创建

  1. <details>
  2. <summary>英文:</summary>
  3. `dplyr` option using `group_by` like this:
  4. ``` r
  5. library(dplyr)
  6. df %&gt;%
  7. group_by(ID_GROUP) %&gt;%
  8. mutate(Random_var = sample(0:1))
  9. #&gt; # A tibble: 4 &#215; 3
  10. #&gt; # Groups: ID_GROUP [2]
  11. #&gt; ID_GROUP ID_INDIVIDUAL Random_var
  12. #&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt;
  13. #&gt; 1 1 1 0
  14. #&gt; 2 1 2 1
  15. #&gt; 3 2 1 1
  16. #&gt; 4 2 2 0

<sup>Created on 2023-05-26 with reprex v2.0.2</sup>

答案3

得分: 2

以下是带有自定义函数的解决方案:

  1. my_binary <- function(n) {
  2. vec <- rep(0, n)
  3. if(n == 1) {
  4. vec <- sample(c(0, 1), 1)
  5. } else {
  6. vec[sample(n, 1)] <- 1
  7. }
  8. return(vec)
  9. }
  10. library(dplyr)
  11. df %>%
  12. group_by(ID_GROUP) %>%
  13. mutate(Random_var = my_binary(n()))
  14. ID_GROUP ID_INDIVIDUAL Random_var
  15. <dbl> <dbl> <dbl>
  16. 1 1 1 0
  17. 2 1 2 1
  18. 3 2 1 0
  19. 4 2 2 1

数据:

  1. df <- data.frame(
  2. ID_GROUP = c(1, 1, 2, 2),
  3. ID_INDIVIDUAL = c(1, 2, 1, 2)
  4. )
英文:

Here is a solution with a custom function:

  1. my_binary &lt;- function(n) {
  2. vec &lt;- rep(0, n)
  3. if(n == 1) {
  4. vec &lt;- sample(c(0, 1), 1)
  5. } else {
  6. vec[sample(n, 1)] &lt;- 1
  7. }
  8. return(vec)
  9. }
  10. library(dplyr)
  11. df %&gt;%
  12. group_by(ID_GROUP) %&gt;%
  13. mutate(Random_var = my_binary(n()))
  14. ID_GROUP ID_INDIVIDUAL Random_var
  15. &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
  16. 1 1 1 0
  17. 2 1 2 1
  18. 3 2 1 0
  19. 4 2 2 1

data:

  1. df &lt;- data.frame(
  2. ID_GROUP = c(1, 1, 2, 2),
  3. ID_INDIVIDUAL = c(1, 2, 1, 2)
  4. )

huangapple
  • 本文由 发表于 2023年5月26日 16:36:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76339081.html
匿名

发表评论

匿名网友

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

确定