R: function assigned to object producing unexpected results -
edit: solved 1 on own. had nothing function object assignment, was assigning results vector "[]" rather list "[[]]"
here's more reading on subject: the difference between [] , [[]] notations accessing elements of list or dataframe
i'm trying filter event data. depending on i'm looking @ i've got filtering different ways. i've got 2 functions use filtering (i use them throughout project, in addition instance):
drop_columns <- function(x, ...) { selectors <- list(...) return(x[ , -which(names(x) %in% selectors)]) } filter_by_val <- function(x, col, ...) { return(x[ which(x[, col] %in% ...), ]) }
here's function choses function filtering, , executes it. note i'm assigning function object called "filter_method":
filter_playtime_data <- function (key_list, data) { filter_method <- null out_list <- list() if(key_list$kind == "games") { filter_method <- function(key_list) { drop_columns(filter_by_val(data, "gametitle", key_list), "x") } } else if (key_list$kind == "skills") { filter_method <- function(key_list) { filter_by_val(data, "skill", key_list) } } # separate data keys out_list["ela"] <- filter_method(key_list[["ela"]]) out_list["math"] <- filter_method(key_list[["math"]]) out_list["sci"] <- filter_method(key_list[["sci"]]) return (out_list) }
i'm trying filter data based on "skills" (ie. using filter_by_val) , it's not working expected. i'm feeding in data.frame , i'm expecting data.frame come out, instead i'm getting list of indexes, if function returning part of function: -which(names(x) %in% selectors)
when run debug browser -- ie. filter_method(key_list[["ela"]]) -- works expected, data frame. values held in output list: out_list[[ela]] list of indexes. idea what's happening?
Comments
Post a Comment