[R] Help needed for efficient way to loop through rows and columns

Rui Barradas ruipbarradas at sapo.pt
Wed May 16 16:15:54 CEST 2012


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.



More information about the R-help mailing list