R – Objects

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.

Type of Objects

There are 5 basic types of objects in the R language:

Vectors

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Create vectors
x <- c(1, 2, 3, 4)
y <- c("a", "b", "c", "d")
z <- 5
  
# Print vector and class of vector
print(x)
print(class(x))
  
print(y)
print(class(y))
  
print(z)
print(class(z))

chevron_right


Output:



[1] 1 2 3 4
[1] "numeric"
[1] "a" "b" "c" "d"
[1] "character"
[1] 5
[1] "numeric"

Lists

List is another type of object in R programming. List can contain heterogeneous data types such as vectors or another lists.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Create list
ls <- list(c(1, 2, 3, 4), list("a", "b", "c"))
  
# Print
print(ls)
print(class(ls))

chevron_right


Output:

[[1]]
[1] 1 2 3 4

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

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

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

[1] "list"

Matrices

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

x <- c(1, 2, 3, 4, 5, 6)
  
# Create matrix
mat <- matrix(x, nrow = 2)
  
print(mat)
print(class(mat))

chevron_right


Output:

     [, 1] [, 2] [, 3]
[1, ]    1    3    5
[2, ]    2    4    6

[1] "matrix"

Factors

Factor object encodes a vector of unique elements (levels) from the given data vector.



Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Create vector
s <- c("spring", "autumn", "winter", "summer"
"spring", "autumn")
  
print(factor(s))
print(nlevels(factor(s)))

chevron_right


Output:

[1] spring autumn winter summer spring autumn
Levels: autumn spring summer winter
[1] 4

Arrays

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Create vectors
x <- 1:5
y <- LETTERS[1:5]
z <- c("Albert", "Bob", "Charlie", "Denver", "Elie")
  
# Create data frame of vectors
df <- data.frame(x, y, z)
  
# Print data frame
print(df)

chevron_right


Output:

  x y       z
1 1 A  Albert
2 2 B     Bob
3 3 C Charlie
4 4 D  Denver
5 5 E    Elie



My Personal Notes arrow_drop_up

Blockchain Enthusiast

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.