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

2012-05-17 Thread David L Carlson
ple[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-4

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

2012-05-16 Thread Priya Bhatt
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: namesXYZAorB S1BBBBBBA S2AABBBBA S3AB

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

2012-05-16 Thread Rui Barradas
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){ i

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

2012-05-16 Thread 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 th