Advantage of a function
To automate certain tasks that you have to repeat over and over
Syntax
<object name> = function(<set of parameters>) {
<set of R statements>
}
Examples
# Firstly, check the new function name
# so that this name is not the built-in functions in R
> getBasicStats
Error: object 'getBasicStats' not found
> getBasicStats = function(x) {
stats = list()
stats$length = length(x) #$ used to reference an item
#in the list by its key name
stats$mean = mean(x)
stats$sd = sd(x)
stats$median = median(x)
unlist(stats) #show stats as a horizontal vector
#instead of as a vertical list
}
> getBasicStats(c(1,2,3,4,5))
length mean sd median
5.000000 3.000000 1.581139 3.000000
/* */
Reading. Practice. Writing.
Please feel free to comment. Suggestions are also welcomed here. Write something "wholesome serving purpose" here.
Subscribe to:
Post Comments (Atom)
If a hater attacked your age and not the goodness of you
Whether young or old, I've always been known what endures. I've known the very idea of people that were all created equal and deserv...
-
DiGi Super Long Life is a feature where you can extend your talktime validity to 1 year for just RM38. Indirectly, the subscription fee is ...
-
Open LibreOffice Draw program. Select the rectangle shape from shape toolbar at the bottom of drawing page. Click a point and drag-and-re...
-
DiGi Super Long Life is a feature where you can extend your talktime validity to 1 year for just RM30. Indirectly, the subscription fee is...
No comments:
Post a Comment