Eshmel
Eshmel

Reputation: 49

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

Upvotes: 1

Views: 90

Answers (2)

Anil Kumar
Anil Kumar

Reputation: 445

you can try dplyr package to solve this, although it is possible without that also.

library(dplyr)

data_frame <- data_frame %>%
  mutate(
    weights = ifelse(group==1,probs1/probs1,
              ifelse(group==2,probs1/probs2,
              ifelse(group==3,probs1/probs3,
              ifelse(group==4,probs1/probs4,NA))))
  )

print(data_frame)

Upvotes: 0

Maurits Evers
Maurits Evers

Reputation: 50668

You can use dplyr::case_when

library(dplyr)
df %>%
    mutate(weights = case_when(
        group == 1 ~ probs1 / probs1,
        group == 2 ~ probs1 / probs2,
        group == 3 ~ probs1 / probs3,
        TRUE ~ probs1 / probs4))
#   region group probs1 probs2 probs3 probs4   weights
#1       1     2    0.2    0.3   0.40   0.10 0.6666667
#2       2     4    0.3    0.4   0.15   0.15 2.0000000
#3       3     3    0.4    0.1   0.30   0.20 1.3333333
#4       4     1    0.7    0.1   0.10   0.10 1.0000000
#5       5     1    0.2    0.3   0.40   0.10 1.0000000
#6       6     2    0.6    0.1   0.10   0.20 6.0000000
#7       7     3    0.7    0.1   0.10   0.10 7.0000000
#8       8     4    0.3    0.2   0.10   0.40 0.7500000
#9       9     3    0.2    0.1   0.10   0.60 2.0000000
#10     10     1    0.1    0.2   0.10   0.60 1.0000000

Sample data

df <- read.table(text =
    "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", header = T)

Upvotes: 1

Related Questions