Skip to content

R help - Adding a column in a data frame with multiple conditions

3 messages · Libby M Gertken, Sarah Goslee, arun

#
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,
By extending Sarah's solution to the whole dataset:
dat1<-read.table(text="
?A B C? D
?1 X 90 88
?1 Y 72 70
?1 Z 67 41
?2 X 74 49
?2 Y 42 50
?2 Z 81 56
?3 X 92 59
?3 Y 94 80
?3 Z 80 82
",sep="",header=TRUE,stringsAsFactors=FALSE) 

dat1$E<-unlist(lapply(split(dat1,dat1$A),function(x) ifelse(x$B=="X"|x$B=="Z",x$C,ifelse(x$B=="Y",x$D,NA))))


?dat1
#? A B? C? D? E
#1 1 X 90 88 90
#2 1 Y 72 70 70
#3 1 Z 67 41 67
#4 2 X 74 49 74
#5 2 Y 42 50 50
#6 2 Z 81 56 81
#7 3 X 92 59 92
#8 3 Y 94 80 80
#9 3 Z 80 82 80
A.K.




----- Original Message -----
From: Sarah Goslee <sarah.goslee at gmail.com>
To: Libby M Gertken <libbymg at utexas.edu>
Cc: r-help at r-project.org
Sent: Thursday, October 4, 2012 4:03 PM
Subject: Re: [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: