I have a simple dataframe, my data, with two variables, A and B. Here's a sample of the first 100 rows:
structure(list(A = c(0, 6, 35, 0, 99, 20, 3, 6, 80, 12, 23, 77,
28, 80, 18, 90, 12, 60, 99, 90, 1, 3, 99, 100, 24, 99, 0, 40,
0, 0, 99, 10, 23, 7, 99, 0, 76, 57, 99, 0, 21, 6, 0, 0, 0, 0,
0, 0, 25, 50, 0, 100, 35, 40, 25, 90, 10, 20, 25, 100, 0, 15,
98, 35, 85, 90, 0, 0, 90, 90, 90, 50, 45, 90, 20, 15, 85, 100,
90, 15, 90, 85, 15, 25, 35, 90, 10, 35, 35, 100, 20, 0, 60, 100,
19, 60, 0, 50, 50, 6), B = c(10, 14, 5, 25, 87, 12, 12, 5, 80,
87, 60, 78, 23, 60, 18, 45, 12, 34, 99, 70, 2, 21, 50, 57, 50,
70, 12, 18, 34, 34, 23, 45, 34, 12, 99, 29, 76, 34, 50, 12, 20,
12, 50, 45, 2, 5, 12, 34, 25, 25, 25, 90, 45, 25, 35, 80, 15,
15, 20, 80, 4, 45, 27, 15, 85, 20, 58, 25, 20, 58, 45, 45, 48,
80, 25, 10, 80, 45, 25, 10, 45, 65, 45, 25, 35, 87, 10, 13, 25,
45, 25, 15, 25, 85, 19, 40, 12, 45, 65, 10)), row.names = 52:151, class = "data.frame")
I want to add a new column for variable P, but the calculation for P differs for three conditions. Such that...
If A < B, then P is equal to (B - A)/(B - 1)
If A > B, then P is equal to (A - B)/(100 - B)
If A = B, then P is equal to 0
How do I apply this logic? I have attempted to use a nested ifelse function as follows:
mydata$P <- ifelse(DF$A < DF$B, ((DF$B-DF$A)/(DF$B - 1)),
ifelse(DF$A == DF$B), 0,
((DF$A-DF$B)/(99 - DF$B)))
But it returns this error:
Error in ifelse(DF$A < DF$B, ((DF$B - DF$A)/(DF$B - 1)), ifelse(DF$A == :
unused arguments (0, ((DF$A - DF$B)/(99 - DF$B)))
Where am I going wrong?
Aucun commentaire:
Enregistrer un commentaire