Download presentation
Presentation is loading. Please wait.
Published bySilvester Dickerson Modified over 9 years ago
1
Programming in R Packages, functions, and apply
2
Functions, Packages, and apply() In this session, we will learn –What are functions. –A brief introduction into user defined functions. What is a package and how to download/install packages Finally, use the apply function to execute a function on a data frame.
3
Functions Most fundamental actions in R are carried out by functions. Examples we have seen –head() –print() –read.csv() –read.table()
4
Functions A very important function is help() –help(mean) –Opens an external webpage that describes the function mean. The mean function: a<- c(1,2,3,4) mean(a)
5
Functions There are many functions already available in base R. -Basic functions -Websites with useful lists of functions NamePurpose min(x)Minimum sd(x)Sample standard deviation round(x, digit)Rounds a number to the specified digits. sum(X)Sums up values
6
Functions Users can create custom functions. Think of functions like Procs and collections of functions as macros. We will create a few functions.
7
Packages People write several specialized functions and the “package” them together. The packages are distributed on the CRAN website and can be accessed directly in R.
8
Packages Dr. Nicole Ferguson has authored a package called ”msSurv”, which provides the functionality necessary to do non-parametric estimation for multistate models. cran.r-project.org/web/packages/msSurv/index.html
9
Apply The apply function takes advantage of the “vectorized” nature of R. –R is fundamentally different from SAS. –SAS processes “records” within a dataset one a time. This makes SAS a very efficient user of memory but not of time. –R processes the entire data object R is fast but does not use memory efficiently.
10
Basic Functions We can substring text variables using the function substr(). More information can be found by typing ?substr at the R prompt. The function is –substr(variable, starts, stop) –Start is a numeric integer that represents the place to start sub-setting –Stop is where we should stop.
11
Examples Will be discussed in the R demo.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.