How to return a value from Nested function to main function in R -


i have been writing function return basic statistics based on few conditions.and arguments data frame , few columns group , 1 arguments check particular value "true or false " , argument pass statistics have extracted mean,median,unique values,sd,var

getstack1<-function(dset,xaxis,color,groupby,aggval,distval){

      dset<-na.omit(dset)       xaxis=dset[,xaxis]       color=dset[,color]       groupby=dset[,groupby]         library(plyr); library(dplyr)       if(distval=="true"){          if(aggval =="count"){            countfunc<-function(dset,xaxis,color,groupby){             library(plyr)             stackval3 <-- ddply(dset, c(xaxis,color), .fun = function(xx){ ln=length(unique(xx[,groupby],na.rm=true))})             assign("stackval3", "stackval4", envir = .globalenv)            }            # a=countfunc(dset,xaxis,color,groupby)           # return(a)            return(stackval3)          }          }     }   

now main function "getstack1", , actual arguments getstack1(sbarr,"workclass","sex","age","count","true")

if(distval=="true") validating aggval,if count, execute inner function countfunc , return value main function. have been facing difficulties return value stackval3 inner main function.

i tried assign value global using stackval3 <--
assign("stackval3", "stackval4", envir = .globalenv) tried call function outside , passing arguments.

a=countfunc(dset,xaxis,color,groupby)

return(a)

but negative. if share methods or code retune value of stackval3 main function.

i have return other statistics using simple if else , gone function because of need unique values. in advance

my data example :

> dput(head(sbarr,10))  structure(list(age = c(39l, 50l, 38l, 53l, 28l, 37l, 49l, 52l,  31l, 42l), workclass = structure(c(8l, 7l, 5l, 5l, 5l, 5l, 5l,  7l, 5l, 5l), .label = c(" federal-gov", " local-gov", " na",  " never-worked", " private", " self-emp-inc", " self-emp-not-inc",  " state-gov", " without-pay"), class = "factor"), fnlwgt = c(77516l,  83311l, 215646l, 234721l, 338409l, 284582l, 160187l, 209642l,  45781l, 159449l), education = structure(c(10l, 10l, 12l, 2l,  10l, 13l, 7l, 12l, 13l, 10l), .label = c(" 10th", " 11th", " 12th",  " 1st-4th", " 5th-6th", " 7th-8th", " 9th", " assoc-acdm", " assoc-voc",  " bachelors", " doctorate", " hs-grad", " masters", " preschool",  " prof-school", " some-college"), class = "factor"), education.num = c(13l,  13l, 9l, 7l, 13l, 14l, 5l, 9l, 14l, 13l), marital.status = structure(c(5l,  3l, 1l, 3l, 3l, 3l, 4l, 3l, 5l, 3l), .label = c(" divorced",  " married-af-spouse", " married-civ-spouse", " married-spouse-absent",  " never-married", " separated", " widowed"), class = "factor"),  occupations = structure(c(1l, 4l, 6l, 6l, 11l, 4l, 9l, 4l,  11l, 4l), .label = c(" adm-clerical", " armed-forces", " craft-repair",  " exec-managerial", " farming-fishing", " handlers-cleaners",  " machine-op-inspct", " na", " other-service", " priv-house-serv",  " prof-specialty", " protective-serv", " sales", " tech-support",  " transport-moving"), class = "factor"), relationship = structure(c(2l,  1l, 2l, 1l, 6l, 6l, 2l, 1l, 2l, 1l), .label = c(" husband",  " not-in-family", " other-relative", " own-child", " unmarried",  " wife"), class = "factor"), race = structure(c(5l, 5l, 5l,  3l, 3l, 5l, 3l, 5l, 5l, 5l), .label = c(" amer-indian-eskimo",  " asian-pac-islander", " black", " other", " white"), class = "factor"),  sex = structure(c(2l, 2l, 2l, 2l, 1l, 1l, 1l, 2l, 1l, 2l), .label = c(" female",  " male"), class = "factor"), capital.gain = c(2174l, 0l,  0l, 0l, 0l, 0l, 0l, 0l, 14084l, 5178l), capital.loss = c(0l,  0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l), hours.per.week = c(40l,  13l, 40l, 40l, 40l, 40l, 16l, 45l, 50l, 40l), native.country = structure(c(40l,  40l, 40l, 40l, 5l, 40l, 23l, 40l, 40l, 40l), .label = c(" cambodia",  " canada", " china", " columbia", " cuba", " dominican-republic",  " ecuador", " el-salvador", " england", " france", " germany",  " greece", " guatemala", " haiti", " holand-netherlands",  " honduras", " hong", " hungary", " india", " iran", " ireland",  " italy", " jamaica", " japan", " laos", " mexico", " na",  " nicaragua", " outlying-us(guam-usvi-etc)", " peru", " philippines",  " poland", " portugal", " puerto-rico", " scotland", " south",  " taiwan", " thailand", " trinadad&tobago", " united-states",  " vietnam", " yugoslavia"), class = "factor"), income = structure(c(1l,  1l, 1l, 1l, 1l, 1l, 1l, 2l, 2l, 2l), .label = c(" <=50k",  " >50k"), class = "factor")), .names = c("age", "workclass",  "fnlwgt", "education", "education.num", "marital.status", "occupations",  "relationship", "race", "sex", "capital.gain", "capital.loss",  "hours.per.week", "native.country", "income"), row.names = c(na,  10l), class = "data.frame") 

there several problems code. first of all, call library inside function, including 2 calls library(plyr), that's not idea. call library outside main function. second, make definition of nested function countfunc depend on 2 if statements, there's no reason that. can/should define @ beginning of main function , make calls depend on if statements, code cleaner. third, don't need assign or <<- @ all, guess, without dataexample , expected output, it's impossible tell sure. (possibly non-working) revision of code be:

library(plyr); library(dplyr)  getstack1<-function(dset,xaxis,color,groupby,aggval,distval){      countfunc<-function(dset,xaxis,color,groupby){         stackval3 <- ddply(dset, c(xaxis,color), .fun = summarize, ln=length(unique(groupby)))         stackval3     }      dset<-na.omit(dset)     stackval3 <- null      if(distval=="true"){         if(aggval =="count"){             stackval3 <- countfunc(dset,xaxis,color,groupby)         }     }     stackval3 }  getstack1(sbarr,"workclass","sex","age","count","true")           workclass     sex ln 1           private  female  1 2           private    male  1 3  self-emp-not-inc    male  1 4         state-gov    male  1 

you can inspire in code above, , post data example , expected output better answer.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -