Formal Arguments in R: Quick Guide

Introduction to Formal Arguments in R

Formal arguments in R are essentially variables you define within a function’s code block. These arguments act as placeholders for the data that are provided when a user uses the function. Formal are the formal arguments of function returned as an object of class pairlist which can be thought of as something similar to a list with an important difference:

is.null(pairlist())
is.null(list())

That is a pairlist of length zero is NULL while a list is not.

Specifying Formal Arguments Positions

Formal arguments in R can be specified by position or by name and we can mix positional matching with matching by name. The following are equivalent.

mean(x = 1:5, trim = 0.1)
mean(1:5, trim = 0.1)
mean(x = 1:5, 0.1)
mean(1:5, 0.1)
mean(trim = 0.1, x = 1:5)
formal arguments in R Language

Functions Formals Default Values

Functions formals may also have the construct symbol=default, which unless differently specified, forces any argument to be used with its default value. Specifically, functions mean() also have a third argument na.rm that defaults to FALSE and as a result, passing vectors with NA values to mean() returns NA.

mean(c(1, 2, NA))

while by specifying na.rm=TRUE we get the mean of all non-missing elements of vector x.

mean(c(1, 2, NA), na.rm = TRUE)

we can redefine mean() function that defaults na.rm to TRUE by simply

mean(c(1, 2, NA))

Now we have a copy of mean.default() in our globalenv:

exists("mean.default", envir = globaenv())

also, notice

environment(mean.default)

The … Argument in a Function

The … argument of a function is special and can contain any number of symbol=value arguments. The … argument is transformed by R into a list that is simply added to the formal list:

h<-function(x, …){
            0
}

formals(h)

The … argument can be used if the number of arguments is unknown. Suppose one wants to define a function that counts the number of rows of any given number of data frames. One can write:

count_rows<-function(…){
     list<-list(…)
     lapply(list, nrow)
}

count_rows(airquality, cars)

By effectively using formal arguments in R Language, one can create reusable and adaptable functions that make the R code more concise and efficient.

https://itfeature.com

https://gmstat.com

Arithmetic Operators in R

Introduction to Arithmetic Operators in R

The article is about making use of arithmetic operators in R Language. The usual arithmetic operations with the usual hierarchy are:

OperatorShort Description
+Addition operator
Subtraction operator
*Multiplication operator
/Division operator
^Exponent operator
# Addition of two or more numbers and varaibles
2 + 7

## output
[1] 9

2.2 + 8.13 + 9

## output
[1] 19.33

Note that, the symbol [1] in the output indicates the element number in this output line.

Defining a Variable in R

Suppose, one wants to define a variable, e.g., $x=5$

R will create an object to store that value and show it when we enter that variable. The object is not displayed on the screen but stored in the active memory of R. The object will be displayed by typing the object name on the R prompt.

To define a variable, we use an assignment operator, i.e., <- (assignment operator) which plays the role of equality (=). For example,

x <- 5
x
Arithmetic Operator and assignment operator in R

The assignment operator (->) may also be used. However, the assignment operator (->) assignment the value from left to right. For example,

x <- 2

will assign the value 2 to variable $x$ and

x ^ 2 -> y

assigns $x^2$ to object (variable) $y$.

Note that by assigning a new value to the same variable, the old value will automatically be over-written (deleted). For example,

x <- 4
x

x <-5
x <- 2

x

Usually, spaces are not required to separate the elements of an arithmetic expression.

Note:

  • If a command is not complete at the end of a line you will see a “+” sign, meaning you must complete the command.
  • Commands are separated by a semi-colon at a new line
  • Comments will be lines starting with a hash mark “#”
  • The interpreter ignores text to the right of #.

Use of Arithmetic Operators in R to Compute a Formula

Question: Find the distance between 2 points (2, 4, 6), and (4.2, 7.1, 8.6).

Solution: The distance between two points say $(x_1,x_2,x_3)$ and $(y_1,y_2,y_3)$ is defined as $$\sqrt{(x_1-y_1)^2 + (x_2-y_2)^2 + (x_3-y_3)^2}$$. The R code may be like:

sqrt((2-4.2)^2 + (4-7.2)^2 + (6-8.6)^2)

# alternatively one can store point values such as
x1 = 2
x2 = 4
x3 = 6
y1 = 4.2
y2 = 7.1
y3 = 8.6

sqrt((x1-y1)^2 + (x2-y2)^2 + (x3-y3)^2)
Arithmetic operators in R and distance between two points

https://itfeature.com

https://rfaqs.com

Customizing R Session

The .Rprofile file is used to customize the R session every time you start it up. The R profile script (.Rprofile) can be created in the home directory. This script gets executed whenever you start a new R session. One can use it to pre-load libraries, set global options, or define custom functions. In this article, we will discuss customizing the R sessions.

Customizing R Session

The R profile script (.Rprofile) file can be used to

  1. Change R’s default,
  2. Define handy command-line functions,
  3. Automatically load your favorite packages

On start-up R will look for the Rprofile in the following places:

1) R Home Directory: R.home() is used to find the directory path in which R is installed.
2) User’s Home Directory: path.expand("~") is used to find the user’s home directory.
3) R Current Working Directory: getwd() is used to find the R’s current working directory.

Modifying R Default Settings

One can employ a few minor modifications on R default settings. For example, The default prompt is > and the output printed in the console is seven numbers after decimals. The following setting will:

  1. Replace the default standard R prompt
  2. Update (reduce) the number of digits from 7 to 4. Note: it does not reduce the precision with which these numbers are internally processed and stored.
  3. The show.signif.stars=FALSE will not show stars to indicate the significance of p-values at the conventional level.
options(prompt = "Imdad> ", digits = 4, show.signif.stars = F)
Customizing R session

Edit Profile using usethis Package

  • Use the usethis::edit_r_profile() function (from the usethis package) to edit your easily .Rprofile.
  • Remember to include sensitive information (like API keys) directly in the script. Consider using a separate
  • The .Renviron file for such cases.
  • If you have both a project-specific .Rprofile and a user-level one, source the user profile at the beginning of your project’s .Rprofile.

Summary

In summary .Rprofile script file allows the user to customize the R environment by setting options, loading libraries, and defining functions that you want available in every session.

Learn about R Workspace, Objects, and .RData File

https://gmstat.com

https://itfeature.com