dede
dede

Reputation: 1171

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

I have a dataframe, 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). 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?

Any suggestion would be very much appreciated. Thank you.

Upvotes: 0

Views: 152

Answers (1)

IRTFM
IRTFM

Reputation: 263332

Desired: "... random assignment of the values 1 and 0, but fulfilling the requirement for five 1 and five 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. It tends to create obscure errors. With tidyverse loaded and not plyr I get:

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. What I didn't understand was whether the situation with Target==1 was correctly assigned. I was thinking you intended to have 10 rows of 1's but this deliver only a single row with Random assigned to 1.

Upvotes: 1

Related Questions