## Special Values in R Programming: A Quick Guide

There are some special values in R Programming language, namely, these are NA, Inf, -inf, NaN, and NULL.

### Special Values in R Programming Language

For numeric variables, several formalized special values are used. The calculations involving special values often result in special values. Regarding statistics, the real-world phenomenon should not include a special value. Therefore, it is desirable to handle special values before performing any statistical, especially inferential analysis. On the other hand, functions in R result in errors or warnings when a variable contains special values.

The NA values in R (NA stands for Not Available) represent the missing observations. A missing value may occur due to the non-response of the respondent or may arise when the vector size is expanded. For example,

v = c(1, 5, 6)
v[5] = 4
v

## Output
[1]  1  5  6 NA  4

To learn about how to handle missing values in R, see the article: Handling Missing Values in R

Inf and -Inf values in R represent a too-big number, which occurs during computation. Inf is for the positive number and -Inf is for the negative number (both represent the positive infinity, and negative infinity, respectively). Inf or -Inf also results when a value or variable is divided by 0. For example,

2 ^ 1024
## Output
[1] Inf

-2^1024

## Output
[1] -Inf

1/0

## Output
[1] Inf

-Inf + 1e10

## Output
[1] -Inf

Sometimes a computation will produce a result that makes little sense. In such cases, R often returns NaN (Not a Number). For example,

Inf - Inf
NaN
0/0

## Output

In R, the Null object is represented by the symbol NULL. It is often used as an argument in functions to represent that no value was assigned to the argument. Additionally, some functions may return NULL. Note that the NULL is not the same as NA, Inf, -Inf, or NaN.

### Getting Information about Special Values

Also, look at the str(), typeof(), and the length of Inf, -Inf, NA, NaN, and Null.

It is worth noting that, the special values in numeric variables indicate values that are not an element of the mathematical set of real numbers. One can use is.finite() function to determine whether the values are regular values or special values. is.finite() function only accepts vector objects. for example,

is.finite(c(1, Inf, NaN, NA))

A function can be written to deal with every numerical column in a data frame. For example,

special <- function(x){
if (is.numeric(x)){
return(!is.finite(x))
}else {
return (is.na(x))
}
}

sapply(airquality, special)

The user defined special() function will test each column of the data frame object (airquality). The function will each special value if the object is numeric, otherwise it only checks for NA.

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:

# 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

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)

https://itfeature.com

https://rfaqs.com

## Operators in R Language Made Easy

### Introduction to Operators in R Language

In R language, different types of operators (symbols) are used to perform mathematical and logical computations. R Language is enriched with built-in operators.

### Operators in R

The types of operators in the R language are:

• Arithmetic Operators
• Relational Operators
• Logical Operators
• Assignment Operators
• Miscellaneous Operators

### Arithmetic Operators in R

The arithmetic operators in R can be used to perform basic mathematical computations (such as addition, subtraction, multiplication, and division) on numbers or elements of the vectors. The following are some examples, related to arithmetic operators.

# Add two vectors
v1 <- c(3,4,5,6)
v2 <- 1:4
print(v1+v2)

# Subtract 2nd vector from 1st
v2 - v1

# Multiply both vectors
v1 * v2

# Divide the 1st vector witht the 2nd
v1/v2

# Compute the remainder by dividing 1st vector with 2nd
v1%%v2

# Compute the Quotient by division of 1st vector with the second
v1%/%v2
# Compute raised to power of other vecotor
v1^v2

### Relational Operators in R

The relational operators are used for comparison purposes. When comparing elements of two vectors, each element of the first vector is compared with the corresponding element of the second vector and results in a Boolean value. The examples are:

# less than comparison
v1 < v2

# greater than comparison
v1 > v2

# exactly equal comparison
v1 == v2

# less than or equal to comparison
v1 <= v2

# greater than or equal to comparison
v1 >= v2

# not equal to comparison
v1 != v2

### Logical Operators in R

The logical operators are used to compare vectors having types of logical (TRUE or FALSE), numeric, or complex numbers. The vectors having values greater than 1 are all considered logical TRUE values.

The examples that make use of logical operators are:

L1 <- c(2, TRUE, 2+2i, FALSE)
L2 <- c(4, 1, 3+1i, TRUE)
# logical AND Operator (Results in TRUE if corresponding elements of vectors are TRUE only)
L1 & L2

# logical OR Operator (Results in TRUE, if either corresponding element of a vector is TRUE
L1 | L2

# logical NOT Operator (Results in opposite logical value)
!L1

The logical operators && and || consider the first element of the vectors and give a vector of a single element as output. The && (AND) operator takes the first element of both the vectors and gives the TRUE only if both elements are TRUE. The || (OR) operator takes the first element of both vectors and gives the TRUE if one of them is TRUE.

# && Operator
L1 && L2

# || Operator
L1 || L2

### Assignment Operators in R

The assignment operators are used to assign values to vectors or variables. The examples are

# <-, =, and <<- assignment operator (Left Assignment)
x1 <- c(3, 5, 6, 7, 8, 9)
x2 =  c(3, 5, 6, 7, 8, 9)
x3 <<-c(3, 5, 6, 7, 8, 9)

# ->, --> (Right Assignment)
x4 -> c(3, 5, 6, 7, 8, 9)
x5 ->>c(3, 5, 6, 7, 8, 9)

### Miscellaneous Operators in R

These operators are used for specific purposes and are not general mathematical or logical computers. These operators include the colon operator, %in% operator, and %*% operator. The Colon operator generates the series of numbers in sequence for a vector. The %in% identifies an element that belongs to a vector and multiplies a matrix with its transpose, matrix multiplication.

# Colon (:) Operator
2:10

# %in% Operator
v1 <- c(5, 6, 4, 7, 8, 9, 2, 3, 4)
4 %in% v1

# %*% Operator
M = matrix(c(3,5,6, 3,2,4), nrow = 2, ncol= 3)
m%*%t(M)

https://itfeature.com

https://rfaqs.com

## How to Round Off Numbers in R: A Comprehensive Guide

The R language is capable of performing from easy to advanced numerical calculations. Although R can compute any computation up to 16 digits accurately, a user may not always want to use (or get) that too many digits in his final results or computations. In such cases, one can use a couple of functions to round off numbers in R Language. To round off a number to two or more digits after the decimal point, one can use the round() function as follows:

### Round off Numbers in R Language

round(123.456,digits = 2)

##
123.46

One can also use the round() function to round off numbers to multiples of 10, 100, and so on. For that purpose, one just needs to add a negative number as the digits argument: For example

round(-123.456,digits = -2)

##
-100

### Significant Digits in R Language

If someone needs to specify the number of significant digits to be retained, regardless of the size of the number, you use the signif() function instead:

signif(-123.456,digits = 4)
##
-123.5

signif(-123.456, digits=3)
##
-123

signif(-123.456, digits=2)
##
-120

Both round() and signif() round off the numbers to the nearest possible number. So, if the first digit that is dropped is smaller than 5, the number is rounded down. If the number is bigger than 5, the number is rounded up. On the other hand, if the first digit that is dropped is exactly 5, R Language uses a rule that is common in programming languages: Always round to the nearest even number. For example, round(1.5) and round(2.5) both return 2, Similarly, for example, round(-4.5) returns -4.

### Rounding off Numbers floor(), ceiling(), and trunc() Functions

Contrary to round(), three other functions always round off the numbers in the same direction:

floor(x) rounds to the nearest integer that is smaller than $x$. So, floor(123.45) becomes 123 and floor(-123.45) becomes â€“124.

ceiling(x) rounds to the nearest integer thatâ€™s larger than $x$. This means ceiling(123.45) becomes 124 and ceiling(-123.45) becomes â€“123.

trunc(x) rounds to the nearest integer in the direction of 0. So, trunc(123.65) becomes 123 and trunc(-123.65) becomes â€“123.

https://itfeature.com

https://gmstat.com