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

R Data Types

R Data Types

A data type refers to a broad system used to declare variables or functions of different types.

The type of a variable determines the space it occupies in storage and how the stored bit pattern is interpreted.

There are three main basic data types in R:

There are two main kinds of numeric constants:

General form 123 -0.125
Scientific notation 1.23e2 -1.25E-1

The logical type is often called Boolean in many other programming languages; its constant values are only TRUE and FALSE。

**Note:** R is case-sensitive; true or True cannot represent TRUE.

The most intuitive data type is the text type. Text is the string (String) commonly seen in other languages, with constants enclosed in double quotes. In R, text constants can be enclosed in either single or double quotes, for example:

Example

'runoob'

==

"runoob"

[

1

]

TRUE

Regarding variable definition in R, unlike the syntax rules in some strongly typed languages that require explicitly setting a name and data type for a variable, every time you use an assignment operator in R, you are actually defining a new variable:

Example

a

=

1

b

<-

TRUE

b

=

"abc"

By object type, there are the following 6 kinds (detailed later):

vector

Vectors are often provided in the standard libraries of dedicated programming languages such as Java, Rust, and C#, because vectors are an indispensable tool in mathematical operations -- the most common vector is the two-dimensional vector, which is inevitably used in a plane coordinate system.

From the perspective of data structures, a vector is a linear list and can be seen as an array.

Having vectors as a type in R makes vector operations much easier:

Example

a

=

c

(

3

,

4

)

b

=

c

(

5

,

0

)

a

b

[

1

]

8

4

c() is a function that creates vectors.

Here we add two two-dimensional vectors to get a new two-dimensional vector (8, 4). If you perform operations on a two-dimensional and a three-dimensional vector, the result loses mathematical meaning; although the program will not stop, a warning will be issued.

I suggest that you make it a habit to avoid such situations.

Each element in a vector can be retrieved individually by its index:

Example

a

=

c

(

10

,

20

,

30

,

40

,

50

)

a

[

2

]

[

1

]

20

**Note:** The "index" in R does not represent an offset, but rather the position (which one), meaning it starts at 1!

R also makes it easy to take a subset of a vector:

Example

a

[

1

:

4

]

Take items 1 through 4, including items 1 and 4

[

1

]

10

20

30

40

a

[

c

(

1

,

3

,

5

)

]

Take items 1, 3, and 5

[

1

]

10

30

50

a

[

c

(

1

,

5

)

]

Remove items 1 and 5

[

1

]

20

30

40

These three subsetting methods are the most commonly used.

Vectors support scalar computation:

Example

c

(

1.1

,

1.2

,

1.3

)

0.5

[

1

]

0.6

0.7

0.8

a

=

c

(

1

,

2

)

a

^

2

[

1

]

1

4

The common math functions described earlier, such as sqrt and exp, can also be used for scalar operations on vectors.

As a linear list structure, a "vector" should have some common linear-list processing functions, and R does have them:

Vector sorting:

Example

a

=

c

(

1

,

3

,

5

,

2

,

4

,

6

)

sort

(

a

)

[

1

]

1

2

3

4

5

6

rev

(

a

)

[

1

]

6

4

2

5

3

1

order

(

a

)

[

1

]

1

4

2

5

3

6

a

[

order

(

a

)

]

[

1

]

1

2

3

4

5

6

The order() function returns a vector of indices after sorting a vector.

Vector statistics

R has a very complete set of statistical functions:

Function name Meaning
sum Sum
mean Compute the mean
var Variance
sd Standard deviation
min Minimum value
max Maximum value
range Range (for a two-dimensional vector: the maximum and minimum values)

Vector statistics example:

Example

sum

(

1

:

5

)

[

1

]

15

sd

(

1

:

5

)

[

1

]

1.581139

range

(

1

:

5

)

[

1

]

1

5

Vector generation

A vector can be generated using the c() function, or you can use the min:max operator to generate a continuous sequence.

If you want to generate an arithmetic sequence with a gap, you can use the seq function:

> seq(1, 9, 2)

[1] 1 3 5 7 9

seq can also generate an arithmetic sequence from m to n; you only need to specify m, n, and the length of the sequence:

> seq(0, 1, length.out=3)

[1] 0.0 0.5 1.0

rep stands for repeat and can be used to produce a sequence of repeated numbers:

> rep(0, 5)

[1] 0 0 0 0 0

NA and NULL are often used in vectors; here we introduce these two terms and their difference:

Example explanation:

Example

length

(

c

(

NA, NA, NULL

)

)

[

1

]

2

c

(

NA, NA, NULL, NA

)

[

1

]

NA NA NA

Obviously, NULL has no meaning in a vector.


Logical type

Logical vectors are mainly used for logical operations on vectors, for example:

Example

c

(

11

,

12

,

13

)

12

[

1

]

FALSE FALSE  TRUE

The which function is a very common logical vector processing function that can be used to filter the indices of the data we need:

Example

a

=

c

(

11

,

12

,

13

)

b

=

a

12

print

(

b

)

[

1

]

FALSE FALSE  TRUE

which

(

b

)

[

1

]

3

For example, we need to filter data greater than or equal to 60 and less than 70 from a linear list:

Example

vector

=

c

(

10

,

40

,

78

,

64

,

53

,

62

,

69

,

70

)

print

(

vector

[

which

(

vector

=

60

&

vector

<

70

)

]

)

[

1

]

64

62

69

Similar functions include all and any:

Example

all

(

c

(

TRUE, TRUE, TRUE

)

)

[

1

]

TRUE

all

(

c

(

TRUE, TRUE, FALSE

)

)

[

1

]

FALSE

any

(

c

(

TRUE, FALSE, FALSE

)

)

[

1

]

TRUE

any

(

c

(

FALSE, FALSE, FALSE

)

)

[

1

]

FALSE

all() checks whether a logical vector is entirely TRUE, and any() checks whether a logical vector contains any TRUE.


String

The string data type itself is not complicated; here we focus on string manipulation functions:

Example

toupper

(

"Runoob"

)

Convert to uppercase

[

1

]

"RUNOOB"

tolower

(

"Runoob"

)

Convert to lowercase

[

1

]

"runoob"

nchar

(

"Chinese"

, type

=

"bytes"

)

Count the byte length

[

1

]

4

nchar

(

"Chinese"

, type

=

"char"

)

Count the total number of characters

[

1

]

2

substr

(

"123456789"

,

1

,

5

)

Extract a substring, from 1 to 5

[

1

]

"12345"

substring

(

"1234567890"

,

5

)

Extract a substring, from 5 to the end

[

1

]

"567890"

as.numeric

(

"12"

)

Convert a string to a number

[

1

]

12

as.character

(

12.34

)

Convert a number to a string

[

1

]

"12.34"

strsplit

(

"2019;10;1"

,

";"

)

Split a string by a delimiter

[

[

1

]

]

[

1

]

"2019"

"10"

 

"1"

gsub

(

"/"

,

"-"

,

"2019/10/1"

)

Replace strings

[

1

]

"2019-10-1"

On Windows computers, the GBK encoding standard is used, so a Chinese character is two bytes; if running on a UTF-8 encoded computer, the byte length of a single Chinese character should be 3.

R supports regular expressions in the Perl language format:

Example

gsub

(

"[[:alpha:]]+"

,

"$"

,

"Two words"

)

[

1

]

"$ $"

For more on strings, refer to:Introduction to R Strings。


Matrix

R provides a matrix type for the study of linear algebra; this data structure is very similar to the two-dimensional arrays in other languages, but R provides language-level support for matrix operations.

First, let's look at matrix creation:

Example

vector

=

c

(

1

,

2

,

3

,

4

,

5

,

6

)

matrix

(

vector

,

2

,

3

)

     

[

,

1

]

[

,

2

]

[

,

3

]

[

1

,

]

   

1

   

3

   

5

[

2

,

]

   

2

   

4

   

6

The initial contents of a matrix are passed in via a vector; next you need to specify how many rows and columns the matrix has.

The values in the vector are filled into the matrix column by column. If you want to fill by row, you need to specify the byrow attribute:

Example

matrix

(

vector

,

2

,

3

, byrow

=

TRUE

)

     

[

,

1

]

[

,

2

]

[

,

3

]

[

1

,

]

   

1

   

2

   

3

[

2

,

]

   

4

   

5

   

6

Every value in a matrix can be accessed directly:

Example

m1

=

matrix

(

vector

,

2

,

3

, byrow

=

TRUE

)

m1

[

1

,

1

]

Row 1, column 1

[

1

]

1

m1

[

1

,

3

]

Row 1, column 3

[

1

]

3

Each column and each row of a matrix in R can be given a name; this is done in batches using character vectors:

Example

colnames

(

m1

)

=

c

(

"x"

,

"y"

,

"z"

)

rownames

(

m1

)

=

c

(

"a"

,

"b"

)

m1

  x y z

a

1

2

3

b

4

5

6

m1

[

"a"

,

]

x y z

1

2

3

The four basic arithmetic operations on matrices are basically the same as for vectors: you can perform operations with a scalar, or element-wise operations with a matrix of the same size.

Matrix multiplication:

Example

m1

=

matrix

(

c

(

1

,

2

)

,

1

,

2

)

m2

=

matrix

(

c

(

3

,

4

)

,

2

,

1

)

m1

%*%

m2

     

[

,

1

]

[

1

,

]

 

11

Inverse matrix:

Example

A

=

matrix

(

c

(

1

,

3

,

2

,

4

)

,

2

,

2

)

solve

(

A

)

     

[

,

1

]

[

,

2

]

[

1

,

]

2.0

 

1.0

[

2

,

]

 

1.5

0.5

solve() function is used to solve linear algebra equations; its basic usage is solve(A,b), whereA is the coefficient matrix of the system of equations,b the vector or matrix of the equation.

apply() function can operate on each row or each column of a matrix as a vector:

Example

(

A

=

matrix

(

c

(

1

,

3

,

2

,

4

)

,

2

,

2

)

)

     

[

,

1

]

[

,

2

]

[

1

,

]

   

1

   

2

[

2

,

]

   

3

   

4

apply

(

A,

1

,

sum

)

With the second parameter set to 1, operate by row using the sum() function

[

1

]

3

7

apply

(

A,

2

,

sum

)

With the second parameter set to 2, operate by column

[

1

]

4

6

For more on matrices, refer to:R Matrices。