0

I have data frame name MC_df, I need to get the list of some variables in MC_df to access through my function, so if I do manually by vari_names_MCdf <- list(MC_df$Age, MC_df$Year, MC_df$Education, MC_df$City, MC_df$Country), it works, but due to the large of data set, I write all of variables' names I want to include into csv file. Now I read csv file to get access all of variables, I got stuck. Here is data MC_df

MC_df <- data.frame(ID=c(1:5),Age=c(18,25,30,22,19),
                Year=c(2003,2010,2008,2010,2015),
                Education = c("<12","13-15",">15","<12",">15"),
                City = c("SD","MS","LA","CV","SD"),
                Country=c("US","CA","CA","CA","US"),
                Group=c(rep("group",5)))

I do manually, just call list all of variables I want, it works

vari_names_MCdf <- list(MC_df$Age, MC_df$Year, MC_df$Education, MC_df$City, MC_df$Country)
vari_group_MCdf <- list(MC_df$Group,MC_df$Group,MC_df$Group,MC_df$Group,MC_df$Group)

Now I write variables into csv file, and read.csv file, it helps me for the futures, I need to edit in csv file, I don't need to access from MC_df

df <- data.frame(names=c("Age","Year","Education","City","Country"),
             text=c("Age Continuos","Year Category","Eduaction Group","City Category","Country Group"))
write.csv(df,"vari.csv")
var <- read.csv("vari.csv", stringsAsFactors = FALSE)
var
  X     names            text
1 1       Age   Age Continuos
2 2      Year   Year Category
3 3 Education Eduaction Group
4 4      City   City Category
5 5   Country   Country Group

I try

vari_names <- as.list(var$names) vari_text <- as.list(var$text)

get("MC_df")$vari_names[[1]]  
vari_names_new <- lapply(vari_names, function(i) paste0("MC_df$",i, sep=""))
get(vari_names_new[[1]])

Everything doesn't work, so how I can active variable names in CSV file and dataset names

BIN
  • 741
  • 7
  • 19
  • Probably easier to deal with a named list that contains data.frames. See gregor's answer to [this post](http://stackoverflow.com/questions/17499013/how-do-i-make-a-list-of-data-frames) for tips. – lmo Mar 16 '17 at 19:17
  • Because I need to return a bunch of characteristics tables and write them into docx file, I use mapply for my function, sometimes I have a to access over 100 variables, with different data set, and variables need to order by report template – BIN Mar 16 '17 at 19:20
  • I got it, use eval(parse(text="variable_name_here")) – BIN Mar 16 '17 at 19:46

0 Answers0