x - data.frame(q33a=3:4,q33b=5:6,q35a=1:2,q35b=2:1)
y - list()
for (i in grep(q33, colnames(x), value=TRUE))
+y[[sub(q33,,i)]] - ifelse(x[[sub(q33,q35,i)]]==1, x[[i]], NA)
as.data.frame(y)
a b
1 3 NA
2 NA 6
# if you really want to create new variables rather
# than have them
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,