ClausW ClausW - 3 months ago 6
R Question

Group data and generate new column

I have data as follows:

1 0 1234
1 0 1235
1 0 5434
2 1 31212
2 1 3212
2 0 1211
3 0 2212
3 0 2212
3 1 1212


What I would like to accomplish using R is to generate a new column, which would have a value of 1 if at least one of the three values (which all belong together) in the second column have a 1. So, my new column would be like:

1 0 1234 0
1 0 1235 0
1 0 5434 0
2 1 31212 1
2 1 3212 1
2 0 1211 1
3 0 2212 1
3 0 2212 1
3 1 1212 1


As each 3 rows belong together, I was not able to figure out how to accomplish this. Could anyone help me with this?

Answer

You can use dplyr and group_by the first column (V1 in my case), and then use any to check if any of the values equals to 1.

library(dplyr)
df %>% 
   group_by(V1) %>% 
   mutate(new = ifelse(any(V2) == 1, 1, 0))

#Source: local data frame [9 x 4]
#Groups: V1 [3]

#     V1    V2    V3   new
#  <int> <int> <int> <dbl>
#1     1     0  1234     0
#2     1     0  1235     0
#3     1     0  5434     0
#4     2     1 31212     1
#5     2     1  3212     1
#6     2     0  1211     1
#7     3     0  2212     1
#8     3     0  2212     1
#9     3     1  1212     1
Comments