Hi,

I would like to extract columns from a dataframe using a vector of desired
column names.

The following working example uses the select argument in the subset
function to accomplish what I am trying to do.  Is there a better solution?

Thanks.

#my data
data <- data.frame("col1"=c(1,2,3),"col2"=c("A","B","C"),"col3"=c(4,5,6))

#names of desired columns
colnames <- c("col1","col3")

fun <- function(colname,dframe){
    nframe <- subset(dframe,select=colname)
    vec <- nframe[,1]
    return(vec)
}

fun(colnames[1],data)
fun(colnames[2],data)

	[[alternative HTML version deleted]]

