An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120516/59abdfde/attachment.pl>
Help needed for efficient way to loop through rows and columns
5 messages · Rui Barradas, Priya Bhatt, David L Carlson
Can you show us what you want the final data.frame to look like? You've created five variables stored as factors and you seem to be trying to change those to numeric values? Is that correct? Since AB and BA are always set to 1, you could just replace those values globally rather than mess with the ifelse commands for those values. Only AA and BB are affected by the value of AorB. Your apply() function processes the data.frame by row so i is a vector consisting of all the values in the row. You seem to be coding as if i was a single integer (as in a for loop). ---------------------------------------------- David L Carlson Associate Professor of Anthropology Texas A&M University College Station, TX 77843-4352
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
project.org] On Behalf Of Priya Bhatt
Sent: Wednesday, May 16, 2012 3:08 AM
To: r-help at r-project.org
Subject: [R] Help needed for efficient way to loop through rows and
columns
Dear R-helpers:
I am trying to write a script that iterates through a dataframe that
looks
like this:
Example dataset called "sample":
names <- c("S1", "S2", "S3", "S4")
X <- c("BB", "AB", "AB", "AA")
Y <- c("BB", "BB", "AB", "AA")
Z <- c("BB", "BB", "AB", NA)
AorB <- c("A", "A", "A", "B")
sample <- data.frame(names, X, Y, Z, AorB)
for a given row,
if AorB == A, then AA == 2, AB = 1, BA = 1, BB = 0
if AorB == B, then AA == 0, AB = 1, BA = 1, BB = 2
I've been trying to write this using apply and ifelse statements in
hopes
that my code runs quickly, but I'm afraid I've make a big mess. See
below:
apply(sample, 1, function(i) {
ifelse(sample$AorB[i] == "A",
(ifelse(sample[i,] == "AA", sample[i,] <- 2 ,
ifelse(sample[i,] == "AB" || sample[i,] == "BA" ,
sample[i,] <- 1,
ifelse(sample[i,] == "BB", sample[i,] <- 0,
sample[i,] <- NA )) )
) , ifelse(sample$AorB[i,] == "B"),
(ifelse(sample[i,] == "AA", sample[i,] <- 0 ,
ifelse(sample[i,] == "AB" || sample[i,] == "BA" ,
sample[i,] <- 1,
ifelse(sample[i,] == "BB", sample[i,] <- 2,
sample[i,] <- NA))))) })
Any Advice?
[[alternative HTML version deleted]]
______________________________________________ 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.
Hello,
Your data.frame is composed exclusively of factors, but try this
(I've changed the name to 'sampl', because 'sample' is an R function.)
# logical index vectors
iA <- sampl$AorB == "A"
iB <- sampl$AorB == "B"
new.sampl <- data.frame(
apply(sampl, 2, function(x){
iAA <- x == "AA"
iBB <- x == "BB"
x[ iA & iAA ] <- 2
x[ iA & iBB ] <- 0
#
x[ iB & iAA ] <- 0
x[ iB & iBB ] <- 2
#
x[ x %in% c("AB", "BA") ] <- 1
x}
))
Hope this helps,
Rui Barradas
Priya Bhatt wrote
Dear R-helpers:
I am trying to write a script that iterates through a dataframe that looks
like this:
Example dataset called "sample":
names <- c("S1", "S2", "S3", "S4")
X <- c("BB", "AB", "AB", "AA")
Y <- c("BB", "BB", "AB", "AA")
Z <- c("BB", "BB", "AB", NA)
AorB <- c("A", "A", "A", "B")
sample <- data.frame(names, X, Y, Z, AorB)
for a given row,
if AorB == A, then AA == 2, AB = 1, BA = 1, BB = 0
if AorB == B, then AA == 0, AB = 1, BA = 1, BB = 2
I've been trying to write this using apply and ifelse statements in hopes
that my code runs quickly, but I'm afraid I've make a big mess. See
below:
apply(sample, 1, function(i) {
ifelse(sample$AorB[i] == "A",
(ifelse(sample[i,] == "AA", sample[i,] <- 2 ,
ifelse(sample[i,] == "AB" || sample[i,] == "BA" ,
sample[i,] <- 1,
ifelse(sample[i,] == "BB", sample[i,] <- 0,
sample[i,] <- NA )) )
) , ifelse(sample$AorB[i,] == "B"),
(ifelse(sample[i,] == "AA", sample[i,] <- 0 ,
ifelse(sample[i,] == "AB" || sample[i,] == "BA" ,
sample[i,] <- 1,
ifelse(sample[i,] == "BB", sample[i,] <- 2,
sample[i,] <- NA))))) })
Any Advice?
[[alternative HTML version deleted]]
______________________________________________ R-help@ 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.
-- View this message in context: http://r.789695.n4.nabble.com/Help-needed-for-efficient-way-to-loop-through-rows-and-columns-tp4630226p4630248.html Sent from the R help mailing list archive at Nabble.com.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120516/461cf1ec/attachment.pl>
This will accomplish what you want and should be relatively easy to modify.
# Create data.fram
names <- c("S1", "S2", "S3", "S4")
X <- c("BB", "AA", "AB", "AA")
Y <- c("BB", "BB", "AB", "AA")
Z <- c("BB", "BB", "AA", NA)
AorB <- c("A", "A", "B", "B")
sample <- data.frame(names, X, Y, Z, AorB,
stringsAsFactors=FALSE)
# Create recoded data.frame
samplemod <- sample
samplemod[,2:4] <- NA
# Recoded values into samplemod
for (i in 1:nrow(sample)) {
for (j in 2:4) {
if (!is.na(sample[i,j])){
if (sample[i, 5] == "A") {
samplemod[i,j] <- switch(sample[i,j], AA = 2,
AB = 1, BA = 1, BB = 0)
}
else {
if (sample[i, 5] == "B") {
samplemod[i,j] <- switch(sample[i,j], AA = 0,
AB = 1, BA = 1, BB = 2)
}
}
}
}
}
----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352
From: Priya Bhatt [mailto:bhattp60 at gmail.com]
Sent: Wednesday, May 16, 2012 1:55 PM
To: dcarlson at tamu.edu; r-help at r-project.org
Subject: Re: [R] Help needed for efficient way to loop through rows and
columns
Yes here it is.? I actually convert them all as strings, initially using??
options(stringsAsFactors=F) at the top of my code.
This what the initial dataframe looks like.? Please note this is a toy
dataset:
names??? X??? Y??? Z??? AorB
S1??? BB??? BB??? BB??? A
S2??? AA ?? BB??? BB??? A
S3??? AB??? AB??? AA??? B
S4??? AA??? AA??? NA??? B
And the code to create this initial dataframe is:
names <- c("S1", "S2", "S3", "S4")
X <- c("BB", "AA", "AB", "AA")
Y <- c("BB", "BB", "AB", "AA")
Z <- c("BB", "BB", "AA", NA)
AorB <- c("A", "A", "B", "B")
sample <- data.frame(names, X, Y, Z, AorB)
The final data.frame should look like:
names? X??? Y??? Z??? AorB
S1??? 0??? 0 ?? 0 ?? A
S2??? 2 ?? 0??? 0 ?? A
S3??? 1 ?? 1 ?? 0 ?? B
S4??? 0 ?? 0 ?? NA B
You're right! - I'll should be able to globally change all ABs and BAs to
1s. Thanks:)? I'm not exactly sure how to change AA and BB depending on AorB
for each row though.? Thoughts?
Thanks for your help thus far, David.
Best, Priya
On Wed, May 16, 2012 at 6:53 AM, David L Carlson <dcarlson at tamu.edu> wrote:
Can you show us what you want the final data.frame to look like? You've created five variables stored as factors and you seem to be trying to change those to numeric values? Is that correct? Since AB and BA are always set to 1, you could just replace those values globally rather than mess with the ifelse commands for those values. Only AA and BB are affected by the value of AorB. Your apply() function processes the data.frame by row so i is a vector consisting of all the values in the row. You seem to be coding as if i was a single integer (as in a for loop). ---------------------------------------------- David L Carlson Associate Professor of Anthropology Texas A&M University College Station, TX 77843-4352
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
project.org] On Behalf Of Priya Bhatt
Sent: Wednesday, May 16, 2012 3:08 AM
To: r-help at r-project.org
Subject: [R] Help needed for efficient way to loop through rows and
columns
Dear R-helpers:
I am trying to write a script that iterates through a dataframe that
looks
like this:
Example dataset called "sample":
names <- c("S1", "S2", "S3", "S4")
X <- c("BB", "AB", "AB", "AA")
Y <- c("BB", "BB", "AB", "AA")
Z <- c("BB", "BB", "AB", NA)
AorB <- c("A", "A", "A", "B")
sample <- data.frame(names, X, Y, Z, AorB)
for a given row,
if AorB == A, then AA == 2, AB = 1, BA = 1, BB = 0
if AorB == B, then AA == 0, AB = 1, BA = 1, BB = 2
I've been trying ?to write this using apply and ifelse statements in
hopes
that my code runs quickly, but I'm afraid I've make a big mess. ?See
below:
apply(sample, 1, function(i) {
? ifelse(sample$AorB[i] == "A",
? ? ? ? ?(ifelse(sample[i,] == "AA", sample[i,] <- 2 ,
? ? ? ? ? ? ? ? ?ifelse(sample[i,] == "AB" || sample[i,] == "BA" ,
sample[i,] <- 1,
? ? ? ? ? ? ? ? ? ? ? ? ifelse(sample[i,] == "BB", sample[i,] <- 0,
sample[i,] <- NA )) )
? ? ? ? ? ) ? , ifelse(sample$AorB[i,] == "B"),
? ? ? ? ?(ifelse(sample[i,] == "AA", sample[i,] <- 0 ,
? ? ? ? ? ? ? ? ?ifelse(sample[i,] == "AB" || sample[i,] == "BA" ,
sample[i,] <- 1,
? ? ? ? ? ? ? ? ? ? ? ? ifelse(sample[i,] == "BB", sample[i,] <- 2,
sample[i,] <- NA))))) })
Any Advice?
? ? ? [[alternative HTML version deleted]]
______________________________________________ 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.