R - using column name as argument for function inside an apply/colwise function -
i have built function determines bins numeric columns , puts output workspace, i'd able apply whole data.frame either apply or colwise function. difficulty have argument needs hold column's name , i'm unsure how go passing column name argument of function being applied?
there post that's similar , answer seems have worked in specific instance i'm unsure how translate function requires name of column.
thank on this!
code , specifics below
#setup library(caret) library(plyr) data(germancredit) germancredit<-germancredit[,-nearzerovar(germancredit)] predictors.a<-germancredit[,1:8] #function applied - note part of large of larger function equalfreqbins.derive<-function(derivedataset,characteristic,targetbins=20, minimum=0, boundarymargin=5){ stopifnot(is.numeric(targetbins),is.numeric(minimum),is.numeric(boundarymargin)) ifelse( is.numeric(derivedataset[,characteristic]) , { truebins<-quantile( derivedataset[,characteristic], seq(0,1,by=(1/targetbins)) ) roundedbins <-round_any(truebins,ifelse(min(truebins)<1,0.01,1),floor) dedupedbins <-unique(roundedbins) assign(characteristic, as.vector(c(ifelse(min(dedupedbins)<minimum,min(dedupedbins)*boundarymargin,minimum), dedupedbins, max(dedupedbins)*boundarymargin)) , envir=as.environment(".globalenv")) }, stop(paste(characteristic,"is non-numeric , cannot binned function")) ) } #here bit i'm struggling numcolwise(equalfreqbins.derive,targetbins=20, minimum=0, boundarymargin=5,derivedataset=predictors.a, characteristic=??)(predictors.a)
without characteristic argument, error
error in `[.data.frame`(derivedataset, , characteristic) : undefined columns selected
based on link, tried
characteristic=names(filter(is.numeric,predictors.a))
but returns , error , assignment ref , purpose of n eludes me
error in fun(x[[1l]], ...) : unused argument(s) (x[[1]])