dat <- data.frame( x = c(1, 2, 3), y = c("a", "b", "c"), z=c(4, 5, 6) )
## all of these return identical output
dat[ names(dat) %in% c("x","z") ]
extract( dat, x, z)
extract( dat, dat$x, dat$z )
## we can even have a variable that includes names
a <- "z"
extract( dat, dat$x, a)
Run the code above in your browser using DataLab