Professional Documents
Culture Documents
Syntax of R program
• A program in R is made up of three things: Variables, Comments, and Keywords. Variables are used to store
the data, Comments are used to improve code readability, and Keywords are reserved words that hold a
specific meaning to the compiler.
• R Data types are used in computer programming to specify the kind of data that can be stored in a variable. For
effective memory consumption and precise computation, the right data type must be selected. Each R data type has
its own set of regulations and restrictions.
• If-else statement: The if-else statement is used to test a condition and execute different code depending on whether
the condition is true or false.
• For loop: The for loop is used to iterate over a sequence of values.
• While loop: The while loop is used to repeatedly execute a block of code until a condition is met.
• Repeat loop: The repeat loop is similar to the while loop, but it does not have a condition. The repeat loop will
continue to execute until a break statement is encountered.
• Next statement: The next statement is used to skip the current iteration of a loop.
• Break statement: The break statement is used to terminate a loop.
• Functions are a way to group together related code so that it can be reused. They are also used to abstract away
complex code, making it easier to read and understand.
• If the variable is still not found, the interpreter will generate an error.
• Here is an example of how scoping works in R:
• x <- 10 # This defines x in the global scope
• f <- function(){
• y <- 20 # This defines y in the local scope of f
• print(x) # This will print the value of x from the global scope
• }
• f() # This calls the function f
• In this example, the value of x that is printed is 10, which is the value of x in the global scope. The value of y is not visible outside of the
function f.
• The environment of a function is the collection of all variables that are visible to the function. The environment of a function is created when the
function is defined.
• When a function is called, the environment of the function is created as a child of the environment in which the function was called.
• This means that the variables in the environment in which the function was called are also visible to the function.
• This is called lexical scoping, and it is the default scoping mechanism in R.
• A Fresh Start
• When a function is called, a new environment is created every time. Each acknowledgement is completely independent because a function cannot tell
what happened when it was run last time.
• Example:
• a <- function(){
• if(!exists("z"))
• {
• z <- 10
• }
• else
• {
• z <- z+10
• }
• z
• }
• a()
• Output:10
There is a function in R which is findGlobals() from codetools and it helps us to find all global variables being used in a function and
lists all the external dependencies of a function. findGlobals() find the global variables and functions which are used by the closure.
Example:
aGlobal <- rnorm(10)
bGlobal <- rnorm(10)
f <- function()
{
a <- aGlobal
b <- bGlobal
plot(b ~ a)
}
codetools::findGlobals(f)
Output:
"{" "~" "<-" "aGlobal" "bGlobal" "plot“
We can manually change the environment to the empty environment emptyenv(). emptyenv() is a totally empty environment.
• makeCacheMatrix(): This function creates a special "matrix" object that can cache its inverse.
• cacheSolve(): This function computes the inverse of the special "matrix" object returned by makeCacheMatrix(). If the
inverse has already been calculated (and the matrix has not changed), then cacheSolve() should retrieve the inverse from the
cache.
cacheMatrix$cacheInverse()