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:
> Hello R Helpers,
>
> After spending considerable time attempting to write a loop (and searching
> the help archives) I have decided to post my problem.
>
> In a dataframe I have columns labeled:
>
> q33a q33b q33c...q33r q35a q35b q35c...q35r
>
> What I want to do is create new variables based on the following logic:
> newfielda <- ifelse(q35a==1, q33a, NA)
> newfieldb <- ifelse(q35b==1, q33b, NA)
> ...
> newfieldr
>
> What I did was create two new dataframes, one containing q33a-r the other
> q35a-r and tried to loop over both, but I could not get any of the loop
> syntax I tried to give me the result I was seeking.
>
> Any help would be much appreciated.
>
> Greg Blevins
> Partner
> The Market Solutions Group, Inc.
> Minneapolis, MN
>
> Windows XP, R 2.1.1
>
> ______________________________________________
> [email protected] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html