R – Objects

10 May, 2020
Every programming language has its own data types to store values or any information so that the user can assign these data types to the variables and perform operations respectively. Operations are performed accordingly to the data types.These data types can be character, integer, float, long, etc. Based on the data type, memory/storage is allocated to the variable. For example, in C language character variables are assigned with 1 byte of memory, integer variable with 2 or 4 bytes of memory and other data types have different memory allocation for them.Unlike other programming languages, variables are assigned to objects rather than data types in R programming.
There are 5 basic types of objects in the R language:
Atomic vectors are one of the basic types of objects in R programming. Atomic vectors can store homogeneous data types such as character, doubles, integers, raw, logical, and complex. A single element variable is also said to be vector.
Example:
# Create vectorsx <- c(1, 2, 3, 4)y <- c("a", "b", "c", "d")z <- 5  # Print vector and class of vectorprint(x)print(class(x))  print(y)print(class(y))  print(z)print(class(z))
Output:
[1] 1 2 3 4
[1] "numeric"
[1] "a" "b" "c" "d"
[1] "character"
[1] 5
[1] "numeric"

List is another type of object in R programming. List can contain heterogeneous data types such as vectors or another lists.
Example:
# Create listls <- list(c(1, 2, 3, 4), list("a", "b", "c"))  # Printprint(ls)print(class(ls))
Output:
[[1]]
[1] 1 2 3 4

[[2]]
[[2]][[1]]
[1] "a"

[[2]][[2]]
[1] "b"

[[2]][[3]]
[1] "c"

[1] "list"

To store values as 2-Dimensional array, matrices are used in R. Data, number of rows and columns are defined in the matrix() function.
Syntax:
matrix(data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL)

Example:
x <- c(1, 2, 3, 4, 5, 6)  # Create matrixmat <- matrix(x, nrow = 2)  print(mat)print(class(mat))
Output:
     [, 1] [, 2] [, 3]
[1, ]    1    3    5
[2, ]    2    4    6

[1] "matrix"

Factor object encodes a vector of unique elements (levels) from the given data vector.
Example:
# Create vectors <- c("spring", "autumn", "winter", "summer", "spring", "autumn")  print(factor(s))print(nlevels(factor(s)))
Output:
[1] spring autumn winter summer spring autumn
Levels: autumn spring summer winter
[1] 4

array() function is used to create n-dimensional array. This function takes dim attribute as an argument and creates required length of each dimension as specified in the attribute.Syntax:
array(data, dim = length(data), dimnames = NULL)

Example:
# Create 3-dimensional array# and filling values by columnarr <- array(c(1, 2, 3), dim = c(3, 3, 3))  print(arr)
Output:
,, 1

     [, 1] [, 2] [, 3]
[1, ]    1    1    1
[2, ]    2    2    2
[3, ]    3    3    3,, 2

     [, 1] [, 2] [, 3]
[1, ]    1    1    1
[2, ]    2    2    2
[3, ]    3    3    3,, 3

     [, 1] [, 2] [, 3]
[1, ]    1    1    1
[2, ]    2    2    2
[3, ]    3    3    3

Data frames are 2-dimensional tabular data object in R programming. Data frames consists of multiple columns and each column represents a vector. Columns in data frame can have different modes of data unlike matrices.
Example:
# Create vectorsx <- 1:5y <- LETTERS[1:5]z <- c("Albert", "Bob", "Charlie", "Denver", "Elie")  # Create data frame of vectorsdf <- data.frame(x, y, z)  # Print data frameprint(df)
Output:
  x y       z
1 1 A  Albert
2 2 B     Bob
3 3 C Charlie
4 4 D  Denver
5 5 E    Elie

Picked
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.