0

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?

0

2 Answers 2

1

We can use matrix subsetting to get values from different columns.

w[cbind(1:nrow(w), y + 1)]
#[1] -0.033 -0.089 -0.030 -0.067 -0.044  0.592 -0.051 -0.026 -0.041

This will select values from column 2 when y = 1 and column 1 when y = 0.

data

w <- structure(c(-0.03272023, -0.0888732, -0.03037798, -0.06745567, 
-0.04390675, -0.10160427, -0.05075316, -0.02616434, -0.04081487, 
0.66042696, 0.60427398, 0.6627692, 0.62569151, 0.64924043, 0.59154291, 
0.64239402, 0.66698285, 0.65233231), .Dim = c(9L, 2L), .Dimnames = list(
NULL, c("w0_7", "w1_7")))

y <- c(0, 0, 0, 0, 0, 1, 0, 0, 0)
Sign up to request clarification or add additional context in comments.

Comments

0

In addition to Ronak's answer, you can use methods for data.frames:

library(dplyr)

# create reproducible example
w = matrix(nrow = 600, ncol = 2)
w[,1] = rnorm(600)
w[,2] = rnorm(600)
y = sample(c(0,1), 600, replace = TRUE)

# data.frame method
wd <- data.frame(w, y)
mutate(wd, out = ifelse(y == 0, X1, X2))

Inspect the results:

head(wd)

          X1          X2 y        out
1 0.30487377 -1.57575230 1 -1.5757523
2 1.09405936 -0.24520181 1 -0.2452018
3 1.27512478 -0.21452914 1 -0.2145291
4 0.53915030 -0.01437150 0  0.5391503
5 0.09399084  0.56232223 1  0.5623222
6 1.80497849 -0.06664301 0  1.8049785

# and your result is accessible via `$`:
head(wd$out)

[1] -1.5757523 -0.2452018 -0.2145291  0.5391503
[5]  0.5623222  1.8049785

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.