Skip to content
Prev 334173 / 398502 Next

How can I apply “Sapply” in R with multiple codes in one function?

There is a no-homework policy on this mailing list (see the Posting Guide mentioned at the bottom of every email). If this is not assigned homework then at the very least you need to understand R and your problem well enough to provide a reproducible example [1] before we can communicate about your problem on this list.

As a start, you should be able to execute each line of each of your examples one at a time. R is interactive and scriptable, so you can always give one line at a time to to R and examine the results. Some results may be printed to the console and then forgotten, while others are stored in objects for use in subsequent lines of code. Try reading and following along with the Introduction to R document that comes with R to learn how R works.

If you do all this, you should come to understand that most of the code in your weibull example seems to be about cleaning up the input data in memory, but I don't know where the functions it references are so you need to get more input from whoever supplied the example about how to use it. It may be as simple as loading a package at the beginning of the code. You would not normally clean the data repeatedly, so most of that code should probably precede your use of sapply.

The weibull function returns an object, and your example code just prints it and discards it. If you want to transfer information from that object into a vector that sapply can bind with a bunch of other vectors to make a matrix, then you will have to assign it into an object and extract those values one at a time as you create that vector.

I highly recommend using the str function on the various objects created by each line of example code at the R console in between execution of the example lines. Use the help facility to learn more:

?str

[1] http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.
kmmoon100 <k.moon at student.unimelb.edu.au> wrote: