this one has been bugging me for a couple of days now, and I havent had any luck on stack exchange yet. Essentially, I have two tables, one table defines what columns (by column number) to select from the second table. My initial plan was to string together the columns and pass that into a subselect statement, however when I define the string as as.character it's not happy, i.e.:
# Data Sets, Variable_Selection: Table of Columns to Select from Variable_Table
VARIABLE_SELECTION <- data.frame(Set.1 = c(3,1,1,1,1), Set.2 = c(0,3,2,2,2), Set.3 = c(0,0,3,4,3),
Set.4 = c(0,0,0,5,4), Set.5 = c(0,0,0,0,5))
VARIABLE_TABLE <- data.frame(Var.1 = runif(100,0,10), Var.2 = runif(100,-100,100), Var.3 = runif(100,0,1),
Var.4 = runif(100,-1000,1000), Var.5 = runif(100,-1,1), Var.6 = runif(100,-10,10))
# Sting rows into character string of columns to select
VARIABLE_STRING <- apply(VARIABLE_SELECTION,1,paste,sep = ",",collapse = " ")
VARIABLE_STRING <- gsub(" ",",",VARIABLE_STRING)
VARIABLE_STRING <- data.frame(VAR_STRING = gsub(",0","",VARIABLE_STRING))
# Will actually be part of lapply function but, one line selection for demonstration:
VARIABLE_SINGLE_SET <- as.character(VARIABLE_STRING[4,])
# Subset table for selected columns
VARIABLE_TABLE_SUB_SELECT <- VARIABLE_TABLE[,c(VARIABLE_SINGLE_SET)]
# Error Returned:
# Error in `[.data.frame`(VARIABLE_TABLE, , c(VARIABLE_SINGLE_SET)) :
# undefined columns selected
I know the text formatting is the problem but I can't find a workaround, any suggestions?