sid<-c(1,2,3,4,5,6,7,8,9,10) q01<-c(2,4,5,2,1,3,2,3,4,5) RDT<-data.frame(SID=sid, Q01=q01) pt1 <- proc.time() for (i in 1:7) RDT$Q01a[RDT$Q01 == i] <- 8-i pt2 <- proc.time() pt2 - pt1 pt1 <- proc.time() RDT$Q01a <- sapply(RDT$Q01, function(x){8-x}) pt2 <- proc.time() pt2 - pt1 test <- data.frame(X = 1:10, Y = LETTERS[1:10]) RDT$Q01b <- sapply(RDT$Q01, function(x){test$Y[which(x == test$X)]})