[R] Function call within a function.
John Kane
jrkrideau at yahoo.ca
Fri Jun 29 13:33:44 CEST 2007
--- Stephen Tucker <brown_emu at yahoo.com> wrote:
> Dear John,
>
> Perhaps I am mistaken in what you are trying to
> accomplish but it seems like
> what is required is that you call lstfun() outside
> of ukn(). [and remove the
> call to lstfun() in ukn()].
>
> nts <- lstfun(myfile, aa, bb)
> results <- ukn(dd1, "a", "b", nts$cda)
Yes this definately will work but seems sloppy. What
I am doing in 'real' life is trying to sort a set of
data.frames into a two element list ( lstfun) and
then apply some further subsetting to each data.frame
in the list separately.
>
> Alternatively, you can eliminate the fourth argument
> in ukn() and assign (via
> '<-') the results of lstfun() to 'nam1' within ukn()
> instead of saving to
> 'nts'...
Humm, I'll have to think about this. The problem, as I
have set up the program is that nam1 is one of the two
data.frames in the list.
I may have to reconsider this whole thing. I have
been trying to use the existing data structures and I
may need to start from scratch.
Thanks for the help. You have given me some useful
ideas.
John
>
> --- John Kane <jrkrideau at yahoo.ca> wrote:
>
> > I am trying to call a funtion within another
> function
> > and I clearly am misunderstanding what I should
> do.
> > Below is a simple example.
> > I know lstfun works on its own but I cannot seem
> to
> > figure out how to get it to work within ukn.
> Basically
> > I need to create the variable "nts". I have
> probably
> > missed something simple in the Intro or FAQ.
> >
> > Any help would be much appreciated.
> >
> > EXAMPLE
> >
>
-------------------------------------------------------------------------------
> > # create data.frame
> > cata <- c( 1,1,6,1,1,4)
> > catb <- c( 1,2,3,4,5,6)
> > id <- c('a', 'b', 'b', 'a', 'a', 'b')
> > dd1 <- data.frame(id, cata,catb)
> >
> > # function to create list from data.frame
> > lstfun <- function(file, alpha , beta ) {
> > cda <- subset(file, file[,1] == alpha)
> > cdb <- subset (file, file[,1]== beta)
> > list1 <- list(cda,cdb)
> > }
> >
> > # funtion to operate on list
> > ukn <- function(file, alpha, beta, nam1){
> > aa <- alpha
> > bb <- beta
> > myfile <- file
> > nts <- lstfun(myfile, aa, bb)
> > mysum <- nam1[,3]*5
> > return(mysum)
> > }
> >
> > results <- ukn(dd1, "a", "b", nts$cda)
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> > http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained,
> reproducible code.
> >
>
>
>
>
>
____________________________________________________________________________________
> Choose the right car based on your needs. Check out
>
More information about the R-help
mailing list