mardi 13 septembre 2016

Assigning test / control group vector using split-apply-combine strategy

this should be simple but it's got me pulling my hair out!

Here is some data:

Clicks <- c(1,2,3,4,5,6,5,4,3,2)
Cost <- c(10,11,12,13,14,15,14,13,12,11)
Cluster <- c(1,1,1,2,2,1,1,1,1,1)
df <- data.frame(Clicks,Cost,Cluster)

I want to filter my df by cluster, assign a new vector that assigns "test" and "control" group at random, then recombine to the original data frame

Step 1: Filter (by cluster 1)

  Clicks Cost Cluster
1      1   10       1
2      2   11       1
3      3   12       1
4      6   15       1
5      5   14       1
6      4   13       1
7      3   12       1
8      2   11       1

Step 2: Assign test and control group at random

  Clicks Cost Cluster   group
1      1   10       1    Test
2      2   11       1 Control
3      3   12       1 Control
4      6   15       1    Test
5      5   14       1 Control
6      4   13       1 Control
7      3   12       1    Test
8      2   11       1 Control

Step 3: Get back to the original df

   Clicks Cost Cluster   group
1       1   10       1    Test
2       2   11       1 Control
3       3   12       1 Control
4       4   13       2    NULL
5       5   14       2    NULL
6       6   15       1    Test
7       5   14       1 Control
8       4   13       1 Control
9       3   12       1    Test
10      2   11       1 Control

Step 4: do the same for cluster 2

Thanks :)




Aucun commentaire:

Enregistrer un commentaire