You can apply a vectorised function to a data frame like so:
f=function(x) ifelse((x>=0 & x<=1),0,ifelse((x>=2 & x<=4),1,NA))
recmat<-f(dat)
or maybe
recmat<-f(dat[,rec])
It would help if you provided an example of what dat
might look like.