samedi 18 novembre 2017

Efficient 50% randomly Choosing value from one of two dataframe columns in r

I have a big dataframe (the following is just an example): I would like to randomly choose (with probabiluty of 50%) either the value of X1 or X2.

> df = data.frame(matrix(rnorm(20), nrow=7))
> df
           X1         X2
1   0.7091409 -1.4061361
2  -1.1334614 -0.1973846
3   2.3343391 -0.4385071
4  -0.9040278 -0.6593677
5   0.4180331 -1.2592415
6   0.7572246 -0.5463655
7  -0.8996483  0.4231117

I would like to randomly choose either from X1 or X2 and add it to as new column.

The result should look something like that:

 > df
               X1         X2     RC
    1   0.7091409 -1.4061361    -1.4061361
    2  -1.1334614 -0.1973846    -1.1334614
    3   2.3343391 -0.4385071    -0.4385071
    4  -0.9040278 -0.6593677    -0.6593677
    5   0.4180331 -1.2592415     0.4180331
    6   0.7572246 -0.5463655     0.7572246
    7  -0.8996483  0.4231117     0.4231117

What is the most efficient way to do it for big data dataframe?




Aucun commentaire:

Enregistrer un commentaire