An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20121004/ee1f87e0/attachment.pl>
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,
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
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:
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 ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.