Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

R Basic Operations

R Basic Operations

This chapter introduces simple operations in the R language.

Assignment

In most languages, assignment uses the = sign, but R is a mathematical language, so its assignment operator is similar to the pseudocode in our math books: a left arrow <- :

Example

a

<-

123

b

<-

456

print

(

a

b

)

The result of running the above code:

[1] 579

This assignment operator is a formal advantage but an operational drawback of R: formally it suits mathematicians better, after all not all mathematicians are used to using = as the assignment operator.

Operationally speaking,< the symbol and the - symbol are both hard to type, which makes many programmers uncomfortable. Therefore, newer versions of R also support = as an assignment operator:

a = 123

b = 456

print(a + b)

This is also a valid R program.

**Note:** It is hard to verify which version of R first supported = assignment, but the R version used throughout this tutorial is 4.0.0.

Arithmetic operators

The following table lists the main arithmetic operators and their order of precedence:

Precedence sign before positive numbers Meaning
1 () Bracket
2 ^ Exponentiation
3 %% Integer division and remainder
  %/% Integer division
4 * Multiplication
  / Division
5 + Addition
  - Subtraction

The following examples demonstrate simple arithmetic operations:

Example

1

2

3

[

1

]

7

(

1

2

)

3

[

1

]

9

3

/

4

[

1

]

0.75

3.4

1.2

[

1

]

2.2

1

4

0.5

^

3

[

1

]

0.5

8

/

3

%%

2

[

1

]

8

8

/

4

%%

2

[

1

]

Inf

3

%%

2

^

2

[

1

]

3

10

/

3

%/%

2

[

1

]

10

Relational operators

The following table lists the relational operators supported by R. Relational operators compare two vectors, comparing each element of the first vector with the corresponding element of the second, and return a Boolean value.

operator Description
> Determine whether each element of the first vector is greater than the corresponding element of the second.
< Determine whether each element of the first vector is less than the corresponding element of the second.
== Determine whether each element of the first vector is equal to the corresponding element of the second.
!= Determine whether each element of the first vector is not equal to the corresponding element of the second.
>= Determine whether each element of the first vector is greater than or equal to the corresponding element of the second.
<= Determine whether each element of the first vector is less than or equal to the corresponding element of the second.

Example

v

<-

c

(

2

,

4

,

6

,

9

)

t

<-

c

(

1

,

4

,

7

,

9

)

print

(

v

t

)

print

(

v

<

t

)

print

(

v

==

t

)

print

(

v

!=

t

)

print

(

v

=

t

)

print

(

v

<=

t

)

The output of the code above is:

[1]  TRUE FALSE FALSE FALSE

[1] FALSE FALSE  TRUE FALSE

[1] FALSE  TRUE FALSE  TRUE

[1]  TRUE FALSE  TRUE FALSE

[1]  TRUE  TRUE FALSE  TRUE

[1] FALSE  TRUE  TRUE  TRUE

Logical operators

The following table lists the logical operators supported by R, which can be used with vectors of numeric, logical, and complex types.

Any non-zero number (positive or negative) is TRUE.

Logical operators compare two vectors, comparing each element of the first vector with the corresponding element of the second, and return a Boolean value.

operator Description
& Element-wise logical AND operator, combining each element of the first vector with the corresponding element of the second; the result is TRUE if both elements are TRUE, otherwise FALSE.
| Element-wise logical OR operator, combining each element of the first vector with the corresponding element of the second; the result is TRUE if either element is TRUE, and FALSE if both are FALSE.
! Logical NOT operator, returning the opposite logical value of each element: FALSE if the element is TRUE, and TRUE if the element is FALSE.
&& Logical AND operator, evaluating only the first element of each vector; the result is TRUE if both elements are TRUE, otherwise FALSE.
|| Logical OR operator, evaluating only the first element of each vector; the result is TRUE if either element is TRUE, and FALSE if both are FALSE.

Example

v

<-

c

(

3

,

1

,TRUE,

2

3i

)

t

<-

c

(

4

,

1

,FALSE,

2

3i

)

print

(

v

&

t

)

print

(

v

|

t

)

print

(

!

v

)

&& and || compare only the first element

v

<-

c

(

3

,

0

,TRUE,

2

2i

)

t

<-

c

(

1

,

3

,TRUE,

2

3i

)

print

(

v

&&

t

)

v

<-

c

(

0

,

0

,TRUE,

2

2i

)

t

<-

c

(

0

,

3

,TRUE,

2

3i

)

print

(

v

||

t

)

The output of the code above is:

[1]  TRUE  TRUE FALSE  TRUE

[1] TRUE TRUE TRUE TRUE

[1] FALSE FALSE FALSE FALSE

[1] TRUE

[1] FALSE

Assignment operators

R variables can be assigned using the leftward, rightward, or equal operator.

The following table lists the assignment operators supported by R.

operator Description

<−

=

<<−

Assign to the left.

−>

−>>

Assign to the right.

Example

Assign to the left

v1

<-

c

(

3

,

1

,TRUE,

"runoob"

)

v2

<<-

c

(

3

,

1

,TRUE,

"runoob"

)

v3

=

c

(

3

,

1

,TRUE,

"runoob"

)

print

(

v1

)

print

(

v2

)

print

(

v3

)

Assign to the right

c

(

3

,

1

,TRUE,

"runoob"

)

->

v1

c

(

3

,

1

,TRUE,

"runoob"

)

->>

v2

print

(

v1

)

print

(

v2

)

The output of the code above is:

[1] "3"      "1"      "TRUE"   "runoob"

[1] "3"      "1"      "TRUE"   "runoob"

[1] "3"      "1"      "TRUE"   "runoob"

[1] "3"      "1"      "TRUE"   "runoob"

[1] "3"      "1"      "TRUE"   "runoob"

Other operators

R also includes some special operators.

operator Description
: The colon operator, used to create a vector of a sequence of numbers.
%in% Used to determine whether an element is in a vector, returning a Boolean value: TRUE if present, FALSE otherwise.
%*% Used to multiply a matrix by its transpose.

Example

A vector from 1 to 10

v

<-

1

:

10

print

(

v

)

Determine whether a number is in vector v

v1

<-

3

v2

<-

15

print

(

v1

%

in

%

v

)

print

(

v2

%

in

%

v

)

Multiply a matrix by its transpose

M

=

matrix

(

c

(

2

,

6

,

5

,

1

,

10

,

4

)

,

nrow

=

2

,

ncol

=

3

,byrow

=

TRUE

)

t

=

M

%*%

t

(

M

)

print

(

t

)

The output of the code above is:



[1]  1  2  3  4  5  6  7  8  9 10

[1] TRUE

[1] FALSE

     [,1] [,2]

[1,]   65   82

[2,]   82  117

Mathematical functions

Some commonly used math functions are:

function Description
sqrt(n) The square root of n
exp(n) e raised to the power of n, where e is the natural constant
log(m,n) The logarithm of m, returning the power to which n must be raised to equal m
log10(m) Equivalent to log(m, 10)

The following examples demonstrate the use of math functions:

Example

sqrt

(

4

)

[

1

]

2

exp

(

1

)

[

1

]

2.718282

exp

(

2

)

[

1

]

7.389056

log

(

2

,

4

)

[

1

]

0.5

log10

(

10000

)

[

1

]

4

Rounding functions:

Name Parameter model Meaning
round (n) Round n to the nearest integer
  (n, m) Round n to m decimal places
ceiling (n) Round n up
floor (n) Round n down

The following examples demonstrate the use of rounding functions:

Example

round

(

1.5

)

[

1

]

2

round

(

2.5

)

[

1

]

2

round

(

3.5

)

[

1

]

4

round

(

4.5

)

[

1

]

4

Note: In some cases, the round function in R may "round down the five".

When the rounding digit is even, the five is also dropped; this differs from the C language.

R's trigonometric functions use radians:

Example

sin

(

pi

/

6

)

[

1

]

0.5

cos

(

pi

/

4

)

[

1

]

0.7071068

tan

(

pi

/

3

)

[

1

]

1.732051

Inverse trigonometric functions:

Example

asin

(

0.5

)

[

1

]

0.5235988

acos

(

0.7071068

)

[

1

]

0.7853981

atan

(

1.732051

)

[

1

]

1.047198

If you have studied probability and statistics, you should be familiar with the following probability distribution functions; because R is designed for mathematicians, these are frequently used:

Example

dnorm

(

0

)

[

1

]

0.3989423

pnorm

(

0

)

[

1

]

0.5

qnorm

(

0.95

)

[

1

]

1.644854

rnorm

(

3

,

5

,

2

)

Generate 3 normal random numbers with mean 5 and standard deviation 2

[

1

]

4.177589

6.413927

4.206032

These four are all functions for computing the normal distribution. Their names all end in norm, standing for "normal distribution".

There are four kinds of prefixes for distribution function names:

Note: Since this tutorial does not focus on mathematical theory, it does not explain in detail the mathematics behind probability distributions. Besides normal distribution functions, R also has common distribution functions such as the Poisson distribution (pois, Poisson); for more detail, you can study "Probability Theory and Mathematical Statistics".