Subset data frame with matrix of logical values

Viewed 3132

Problem

I have data on two measures for four individuals each in a wide format. The measures are x and y and the individuals are A, B, C, D. The data frame looks like this

d <- data.frame(matrix(sample(1:100, 40, replace = F), ncol = 8))
colnames(d) <- paste(rep(c("x.", "y."),each = 4), rep(LETTERS[1:4], 2), sep ="")
d

  x.A x.B x.C x.D y.A y.B y.C y.D
1  56  65  42  96 100  76  39  26
2  19  93  94  75  63  78   5  44
3  22  57  15  62   2  29  89  79
4  49  13  95  97  85  81  60  37
5  45  38  24  91  23  82  83  72

Now, would I would like to obtain for each row is the value of y for the individual with the lowest value of x.

So in the example above, the lowest value of x in row 1 is for individual C. Hence, for row 1 I would like to obtain y.C which is 39.

In the example, the resulting vector should be 39, 63, 89, 81, 83.

Approach

I have tried to get to this by first generating a matrix of the subset of d for the values of x.

t(apply(d[,1:4], 1, function(x) min(x) == x))

       x.A   x.B   x.C   x.D
[1,] FALSE FALSE  TRUE FALSE
[2,]  TRUE FALSE FALSE FALSE
[3,] FALSE FALSE  TRUE FALSE
[4,] FALSE  TRUE FALSE FALSE
[5,] FALSE FALSE  TRUE FALSE

Now I wanted to apply this matrix to subset the subset of the data frame for the values of y. But I cannot find a way to achieve this.

Any help is much appreciated. Suggestions for a totally different - more elegant - approach are highly welcome too.

Thanks a lot!

3 Answers
Related