Suppose i have a data set with 600 observations named w.
> w
w0_7 w1_7
[1,] -0.03272023 0.66042696
[2,] -0.08887320 0.60427398
[3,] -0.03037798 0.66276920
[4,] -0.06745567 0.62569151
[5,] -0.04390675 0.64924043
[6,] -0.10160427 0.59154291
[7,] -0.05075316 0.64239402
[8,] -0.02616434 0.66698285
[9,] -0.04081487 0.65233231
[10,] -0.04081487 0.65233231
[11,] -0.02251459 0.67063259
[12,] -0.02616434 0.66698285
[13,] -0.02819600 0.66495118
[14,] -0.07752280 0.61562439
[15,] -0.47663409 0.21651309
[16,] -0.03523314 0.65791404
[17,] -0.02251459 0.67063259
[18,] -0.02251459 0.67063259
[19,] -0.03037798 0.66276920
[20,] -0.03037798 0.66276920
...
and this other data set called Y, consisting of 600 1's and 0's.
[1] 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[70] 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[139] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 ...
If Y[i]=="1", i want to attach the corresponding w1_7[i] to it, and if Y[i]=="0", i want to attach the corresponding w0_7[i] value. So the expected output will be for example
>Y
[1] 0 0 1
> w
w0_7 w1_7
[1,] -0.03272023 0.66042696
[2,] -0.08887320 0.60427398
[3,] -0.03037798 0.66276920
> output
[1] -0.03272023 -0.08887320 0.66276920
Anyone knows how to code this in R?