vendredi 5 octobre 2018

multiple if conditions in r

I have been working on this problem, but failed to solve it. I know the answer might be very easy, but I could not solve it and could not find how to solve it from other similar questions as well.

I have got the following data.frame:

region    group   probs1   probs2   probs3   probs4     weights
   1        2       0.2       0.3     0.4       0.1        NA
   2        4       0.3       0.4     0.15      0.15       NA
   3        3       0.4       0.1     0.3       0.2        NA
   4        1       0.7       0.1     0.1       0.1        NA
   5        1       0.2       0.3     0.4       0.1        NA
   6        2       0.6       0.1     0.1       0.2        NA
   7        3       0.7       0.1     0.1       0.1        NA
   8        4       0.3       0.2     0.1       0.4        NA
   9        3       0.2       0.1     0.1       0.6        NA
  10        1       0.1       0.2     0.1       0.6        NA

What I am going to do is to create a new column in the data.frame called "weights" that is calculated as if group==1, then weights=probs1/probs1. If group==2, then the weights=probs1/probs2. If group==3, then the weights=probs1/probs3. If group==4, then the weights=probs1/probs4.

I used different types of codes like, ifelse, if....else, dplyr, but I failed. In fact, may codes only create the weights for weights=probs1/probs1 and apply it for all the regions, regardless of the group.

May someone please help me to solve it? Thanks

Aucun commentaire:

Enregistrer un commentaire