Libby M Gertken
2012-Oct-04 18:47 UTC
[R] R help - Adding a column in a data frame with multiple conditions
Hi, I am trying to add a column of numbers to a data frame in R with multiple conditions. Here is a simplified example df: [A] [B] [C] [D] [E] [1] 1 X 90 88 [2] 1 Y 72 70 [3] 1 Z 67 41 [4] 2 X 74 49 [5] 2 Y 42 50 [6] 2 Z 81 56 [7] 3 X 92 59 [8] 3 Y 94 80 [9] 3 Z 80 82 I would like column [E] to have a certain value (found either in [C] or [D]) based on conditions in columns [A] *and* [B]. E.g. : if [A] = 1 and [B] = X, then [E] = the entry in [C] for that row (i.e., 90) if [A] = 1 and [B] = Y, then [E] = the entry in [D] for that row (i.e., 70) if [A] = 1 and [B] = Z, then [E] = the entry in [C] for that row (i.e., 67) if [A] = 2 and [B] = X, then [E] = the entry in [C] for that row (i.e., 74) if [A] = 2 and [B] = Y, then [E] = the entry in [D] for that row (i.e., 50) if [A] = 2 and [B] = Z, then [E] = the entry in [C] for that row (i.e., 81) and so on. ATTEMPT TO RESOLVE: The following code allowed me to add values for column [E] when [A] ==1, but I can't figure out how to keep the code going in order to get a value for column [E] based on all of the numbers in column [A] and the secondary condition for [B] ([A] goes from 1:48). df$[E] <- ifelse((df$A == 1) & (df$B == "X"), df$C[df$A == 1], ifelse((df$A == 1) & (df$B == "Y"), df$D[df$A == 1], ifelse((df$A == 1) & (df$B == "Z"), df$C[df$A == 1], NA)))) Thank you for any advice you can give. Libby G libbymg[at]utexas.edu [[alternative HTML version deleted]]
Sarah Goslee
2012-Oct-04 20:03 UTC
[R] R help - Adding a column in a data frame with multiple conditions
Hi Libby, You had an accumulation of small errors, from an extra ) to an unclear understanding of how indexing works in R. Also, you shouldn't call your dataframe df, or use square brackets in column names. That said, what about: sampledata <- structure(list(A = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L), B = c("X", "Y", "Z", "X", "Y", "Z", "X", "Y", "Z"), C = c(90L, 72L, 67L, 74L, 42L, 81L, 92L, 94L, 80L), D = c(88L, 70L, 41L, 49L, 50L, 56L, 59L, 80L, 82L)), .Names = c("A", "B", "C", "D"), class "data.frame", row.names = c(NA, -9L)) E <- ifelse((sampledata$A == 1) & (sampledata$B == "X"), sampledata$C, ifelse((sampledata$A == 1) & (sampledata$B == "Y"), sampledata$D, ifelse((sampledata$A == 1) & (sampledata$B == "Z"), sampledata$C, NA))) sampledata <- data.frame(sampledata, E) Sarah On Thu, Oct 4, 2012 at 2:47 PM, Libby M Gertken <libbymg at utexas.edu> wrote:> Hi, > > I am trying to add a column of numbers to a data frame in R with multiple > conditions. > > Here is a simplified example df: > > [A] [B] [C] [D] [E] > [1] 1 X 90 88 > [2] 1 Y 72 70 > [3] 1 Z 67 41 > [4] 2 X 74 49 > [5] 2 Y 42 50 > [6] 2 Z 81 56 > [7] 3 X 92 59 > [8] 3 Y 94 80 > [9] 3 Z 80 82 > > I would like column [E] to have a certain value (found either in [C] or > [D]) based on conditions in columns [A] *and* [B]. > > E.g. : > if [A] = 1 and [B] = X, then [E] = the entry in [C] for that row (i.e., 90) > if [A] = 1 and [B] = Y, then [E] = the entry in [D] for that row (i.e., 70) > if [A] = 1 and [B] = Z, then [E] = the entry in [C] for that row (i.e., 67) > > if [A] = 2 and [B] = X, then [E] = the entry in [C] for that row (i.e., 74) > if [A] = 2 and [B] = Y, then [E] = the entry in [D] for that row (i.e., 50) > if [A] = 2 and [B] = Z, then [E] = the entry in [C] for that row (i.e., 81) > > and so on. > > ATTEMPT TO RESOLVE: > > The following code allowed me to add values for column [E] when [A] ==1, > but I can't figure out how to keep the code going in order to get a value > for column [E] based on all of the numbers in column [A] and the secondary > condition for [B] ([A] goes from 1:48). > > df$[E] <- > > > ifelse((df$A == 1) & (df$B == "X"), df$C[df$A == 1], > > ifelse((df$A == 1) & (df$B == "Y"), df$D[df$A == 1], > > ifelse((df$A == 1) & (df$B == "Z"), df$C[df$A == 1], > > NA)))) > > > Thank you for any advice you can give. > > > Libby G > > libbymg[at]utexas.edu > > [[alternative HTML version deleted]] >-- Sarah Goslee http://www.functionaldiversity.org