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)
AI Doomsday
AI apocalypse AI extinction risk AI catastrophic AI was going to be the end of humanity. AI could kill all humans! AI leading to human extin...
-
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...
-
Subject: Use CodeBlocks to generate Nassi–Shneiderman Diagrams (NSDs) automatically What is the Nassi-Shneiderman Diagram? The Nassi-Shneide...
-
Open LibreOffice Draw program. Select the rectangle shape from shape toolbar at the bottom of drawing page. Click a point and drag-and-re...
No comments:
Post a Comment