nmori12
nmori12

Reputation: 29

How to subset data based on a column efficiently (R)

This is my first time posting, so bear with me on the formatting. I am trying to select data based on an another columns data efficiently.

The data set is as follows:

+------+------+------+------+--+
| Var1 | Var2 | Var3 | Type |  |
+======+======+======+======+==+
|  3.3 |  2.3 |  5.5 |    1 |  |
|  3.4 |  2.0 |  7.5 |    2 |  |
|  2.9 |  1.9 |  6.2 |    1 |  |
|  3.1 |  2.2 |  5.9 |    1 |  |
|  3.3 |  2.1 |  7.1 |    3 |  |
+------+------+------+------+--+

In row 1, when Type = 1, I want it to select that rows Var1 and put it in an another column, for Type = 2, Var2, Type = 3, Var3 -- and so on. How would I do this efficiently? I have done it with a for loop, but with a bigger dataset it takes a while to process. Thanks

If df[i, 4] == 1, then df[i, 5] == df[i, 1]

Upvotes: 1

Views: 35

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388982

We can do matrix subsetting.

df$new_col <- df[cbind(1:nrow(df), df$Type)]
df
#  Var1 Var2 Var3 Type new_col
#1  3.3  2.3  5.5    1     3.3
#2  3.4  2.0  7.5    2     2.0
#3  2.9  1.9  6.2    1     2.9
#4  3.1  2.2  5.9    1     3.1
#5  3.3  2.1  7.1    3     7.1

data

df <- structure(list(Var1 = c(3.3, 3.4, 2.9, 3.1, 3.3), Var2 = c(2.3, 
2, 1.9, 2.2, 2.1), Var3 = c(5.5, 7.5, 6.2, 5.9, 7.1), Type = c(1L, 
2L, 1L, 1L, 3L)), class = "data.frame", row.names = c(NA, -5L))

Upvotes: 1

Related Questions