🥳Data Type

A data type, in programming, is a classification that specifies which type of value a variable has and what type of mathematical, relational, or logical operations can be applied to it without causing an error.

There are mainly 5 data types in R:

  1. Vector

  2. Matrix

  3. Array

  4. List

  5. Data Frame

1. Vector

A vector is a sequence of data elements of the same basic type. The are 5 classes of vectors.

In R vectors are denoted by c().

  1. Logical

    Ex: True or False

  2. Integer: The whole number values.

    Ex: 1, 2, 5, 100, 20L, 15L, etc.

  3. Numeric: Both whole numbers and decimal values.

    Ex: 4, 3.1416, 0.534, etc.

  4. Complex

    Ex: 3+4i, 5+2i, etc.

  5. Character: Needs to be enclosed between single or double quotes.

    Ex: "M", "We", "Someone", etc.

"We can use the L suffix to qualify any number with the intent of making it an explicit integer"

To check the type/class of a vector use class(vectorName).

Code Example:

VactorName <- c("We", "love", "R", "programming")

Do not use more than one class of vector in a single vector.

2. Matrix

Matrix is the R object in which the elements are arranged in a two-dimensional rectangular layout.

matrix(data, nrow, ncol, byrow, dimnames)

Syntax Breakdown:

  • data: is the input vector which becomes the data elements of the matrix.

  • nrow: is the number of rows to be created.

  • ncol: is the number of columns to be created.

  • byrow: is a logical clue. If TRUE then the input vector elements will be arranged by row.

  • dimnames: are the names assigned to the rows and columns.

Code Example Output:

3. Array

Arrays are the R data objects which can store data in more than two dimensions.

array(data, dim, dimnames)

4. List

Lists are the R objects that contain elements of different types like - numbers, strings, vectors, and other lists inside them.

In simple words, a list can contain more than one type of data.

listName <- list(data)

Code Output:

4. DataFrame

A dataframe is a table or two-dimensional array-like structure in which each column contains values of one variable and each row contains one set of values from each column.

data.frame(data, row.names = NULL, stringsAsFactors = FALSE)

Syntax Breakdown:

  • data: can be a matrix, table, etc.

  • row.names = NULL: Whether you want to specify a column name that will be used as row names. Unless specified (NULL) row names will be integer numbers.

  • stringsAsFactors = FALSE: If TRUE, the columns with character values will be considered as a factor.

Code Output:

R contains a number of preloaded datasets. You can load and use them instantly.

  • To view the built-in datasets in R, use the following command: data()

  • To load these datasets in your RStudio environment use data(datasetName).

To learn more about dataframe and their manipulation view the following page:

pageDataFrame

Sources of the contents on this page:

Last updated