Skip to content
Prev 74873 / 398502 Next

Seeking help with a loop

You can do the following without resorting to a "hard coded" loop
sapply( paste("q35", letters[1:grep("r", letters)], sep=""), function(x)
ifelse(temp[, x]%in%1,temp[, sub("5", "3", x)],NA)


as the following example shows
temp <- matrix(sample(c(0,1), 360, replace=T), nrow=10)
 colnames(temp) <- c(paste("q33", letters[1:grep("r", letters)], sep=""), paste("q35", letters[1:grep("r", letters)], sep=""))
 sapply( paste("q35", letters[1:grep("r", letters)], sep=""), function(x)  ifelse(temp[, x]%in%1,temp[, sub("5", "3", x)],NA))


HTH

Jean
On Wed, 3 Aug 2005, Greg Blevins wrote: