简体   繁体   English

如何根据条件和出现次数分配R中的两个值之一

[英]how to assign one of two values in R based on conditions and number of occurrences

I have a dataframe, DF2 . 我有一个数据框DF2 Here a reproducible example of a short version of my dataframe: 这是我的数据框的简短版本的可复制示例:

Scene2 = rep(c(1:10), times=9)
myDF2 <- data.frame(Scene2)
myDF2$Target <- rep(0,10, each=9)
myDF2$Target[myDF2$Scene2==7] <- 1 #actually, in my dataframe Scene2 could be equal to any number (not always 7) for Target to be equal to 1, but for simplicity I created this reproducible code.
myDF2$Trial <- rep(c(1:9),each=10)
myDF2$Route <- rep(LETTERS[1:6], each=10, length=nrow(myDF2))

I would like to create a new column Random , such that for each Trial and Route , if Target is equal to 0, then the value in Random could randomly be either 1 or 0. The important thing is that for each Trial and Route I end up with five 1, and five 0 (and when Target is equal to 1, then Random has always to be 1). 我想创建一个新列Random ,以便对于每个Trial and Route ,如果Target等于0,则Random中的值可以随机为1或0。重要的是,对于每个TrialRoute I结束由五个1和五个0组成(当Target等于1时, Random必须始终为1)。 The following code works, but the order doesn't look random. 以下代码有效,但是顺序看起来不是随机的。

library(plyr)
myDF3 <- myDF2 %>% group_by(Trial, Route) %>%
 mutate(Random = ifelse(myDF2$Target==0,sample(c(0,1),replace=T, prob=c(0.5,0.5)),1)) %>% as.data.frame()

This gives me as result: 结果就是这样:

Scene2  Target  Trial   Route   Random     #I would like something more random, just an example:
1       0       1       A       1          #0
2       0       1       A       0          #0
3       0       1       A       1          #0
4       0       1       A       0          #0
5       0       1       A       1          #0
6       0       1       A       0          #1
7       1       1       A       1          #1
8       0       1       A       0          #1
9       0       1       A       1          #1
10      0       1       A       0          #1
1       0       2       B       1          #1
2       0       2       B       0          #0
3       0       2       B       1          #1
4       0       2       B       0          #0
5       0       2       B       1          #1
6       0       2       B       0          #0
7       1       2       B       1          #1
8       0       2       B       0          #0
9       0       2       B       1          #1
10      0       2       B       0          #0
1       0       3       C       1          #1
2       0       3       C       0          #1
3       0       3       C       1          #0
4       0       3       C       0          #0
5       0       3       C       1          #1
6       0       3       C       0          #0
7       1       3       C       1          #1
8       0       3       C       0          #0
9       0       3       C       1          #1
10      0       3       C       0          #0
1       0       4       D       1          #1
2       0       4       D       0          #1
3       0       4       D       1          #1
4       0       4       D       0          #1
5       0       4       D       1          #0
6       0       4       D       0          #0
7       1       4       D       1          #1
8       0       4       D       0          #0
9       0       4       D       1          #0
10      0       4       D       0          #0

How to create a more random assignment of the values 1 and 0, but fulfilling the requirement for five 1 and five 0? 如何创建值1和0的更随机分配,但又满足五个1和五个0的要求?

Any suggestion would be very much appreciated. 任何建议将不胜感激。 Thank you. 谢谢。

Desired: "... random assignment of the values 1 and 0, but fulfilling the requirement for five 1 and five 0" 期望:“ ...随机分配值1和0,但满足五个1和五个0的要求”

Strategy: That's basically a request for a "permutation of a vector" 策略:这基本上是对“向量置换”的要求

 set.seed(123) # needed for reproducibility
 sample( c(rep(1,5),rep(0,5) ) )
 #[1] 1 0 1 0 0 1 0 0 1 1

You probably should not be using library(plyr) within the tidyverse. 您可能不应该在tidyverse中使用library(plyr) It tends to create obscure errors. 它往往会产生模糊的错误。 With tidyverse loaded and not plyr I get: 加载tidyverse而不是plyr我得到:

myDF3 <- myDF2 %>% group_by(Trial, Route) %>%
                      mutate(Random = ifelse(Target==0,
                                    sample(c(rep(0,5),rep(1,5))), 
                                    rep(1,10) )) %>% 
                      as.data.frame()

I'm not sure that's waht was wanted although it's got the permutation in the case of Target==0 correct. 我不确定这是不是想要的,尽管在Target == 0的情况下可以进行排列。 What I didn't understand was whether the situation with Target==1 was correctly assigned. 我不明白的是,是否正确分配了Target == 1的情况。 I was thinking you intended to have 10 rows of 1's but this deliver only a single row with Random assigned to 1. 我当时以为您打算有10行的1,但这只能传递一行,而Random分配为1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据R中的条件将值随机分配给两个矩阵 - randomly assign values to two matrix based on conditions in R R 根据多个条件分配值 - R Assign Values based on multiple conditions 在 R 中,如何根据两个条件对一列的值求和,并按另一列值分组? - How to sum values of one column, based on two conditions, grouped by another column value, in R? 如何根据R中的两列分配增量值? - How to assign incremental values based on two columns in R? 根据 R 中的两列分配有序数 - Assign the ordered number based on two columns in R 从向量中选取一个数字并根据R中的多个条件分配给列 - Picking a number from vector and assign to column based on multiple conditions in R 根据R中的多个条件分配新的列值 - Assign new column values based on multiple conditions in R 根据 R 中其他向量的条件,将值分配给 dataframe 中的向量 - assign values to vector in a dataframe based on conditions from other vectors in R 根据 R 中两个其他向量中的匹配值在一个向量中分配值 - Assign values in one vector based on matching values in two other vectors in R 如何根据一列中的值对数据进行装箱,并汇总R中另一列中的出现次数? - How to bin data based on values in one column, and sum occurrences from another column in R?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM