R: variable name to string -


there simple solutions such as:

> deparse(substitute(data)) [1] "data" 

but not quite work when want extract name list:

> deparse(substitute(names(data)[1])) [1] "names(data)[1]" 

so problem want general solution every variable in list/data frame/numeric feed function. , want see names of variables column names output data frame.

such as:

foo <- function(...) {     data <- list(...) ## magic here ##    names(output_df) <- ??     output_df } 

and keep in mind numerics fed ... don't come names attribute. whole reason why want use environment name column name in output data frame.

so apparently, "??" in question above should substituted with:

setdiff(as.character(match.call(expand.dots=true)),          as.character(match.call(expand.dots=false))) 

with expanded example:

num_vec <- as.numeric(1:10) list1 <- 1:10 list2 <- 11:20 list_data <- list(list1, list2) df_data <- data.frame(list1, list2)  foo <- function(...) {   input_list <- list(...)   name_vec <- setdiff(as.character(match.call(expand.dots=true)),                        as.character(match.call(expand.dots=false)))   output_df <- list()   for(i in 1:length(input_list)) {     output_df <- c(output_df, input_list[i])   }   output_df <- data.frame(output_df)   names(output_df) <- name_vec   output_df }  foo(num_vec, list_data[[1]], df_data[[1]], df_data$list2)     num_vec list_data[[1]] df_data[[1]] df_data$list2 1        1              1            1            11 2        2              2            2            12 3        3              3            3            13 4        4              4            4            14 5        5              5            5            15 6        6              6            6            16 7        7              7            7            17 8        8              8            8            18 9        9              9            9            19 10      10             10           10            20 

this intention - data sort of source, manipulations (not shown in example not related problem) , data frame output column names appear in function variables.

thanks peyton pointing out solution in other post failed find through search.


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -