0

I have the following data frame:

A  P(A)
1  0.8
0  0.7
1  0.5
1  0.8
1  0.9
0  0.7
0  0.8

For all 0's in column A, I would like to calculate 1-P(A). This would give me the following dataset:

A  P(A)
1  0.8
0  0.3  ## 1 - 0.7
1  0.5
1  0.8
1  0.9
0  0.3  ## 1 - 0.7
0  0.2  ## 1 - 0.8

I tried this but it doesn't give me the required output.

  for(i in nrow(results)) {
    if(results[i,1]==0) {
      results[i,2]<- 1-results[i,2] 
    }
  }

How is this possible in R?

AngryPanda
  • 1,201
  • 2
  • 19
  • 41

1 Answers1

1
df <- data.frame(A = c(1,0,1,1,1,0,0), P = c(0.8, 0.7, 0.5,0.8, 0.9,0.7,0.8))

df[df$A == 0, "P" ] <- 1 - df[df$A == 0, "P"]
Jacob H
  • 3,917
  • 2
  • 29
  • 36