R works with various data types:
3.14159
(pi), 100.5
(temperature)2024
(current year), 42
(the answer to the ultimate question of life, the universe, and everything)TRUE
(statement is correct), FALSE
(statement is false)T
and F
also mean true and false, respectively in R"Hello, world!"
, "This is some text data"
These data types look something like this in code:
Similarly, R also works with different data structures including
c(1, 3.14, "apple")
(combines numeric, integer, and character elements)c()
function stands for concatenate or combinematrix(1:9, nrow = 3, ncol = 3)
(creates a 3x3 matrix with numbers 1 to 9)array(1:24, dim = c(2, 3, 4))
(creates a 2x3x4 array with numbers 1 to 24)list(name = "Alice", age = 30, hobbies = c("reading", "hiking"))
(combines character, numeric, and vector elements)data.frame(ID = 1:3, Name = c("Bob", "Charlie", "David"), Age = c(25, 32, 41))
(creates a data frame with numeric IDs, character names, and numeric ages)These data structures look something like this in code:
Understanding the data type is crucial for choosing appropriate functions to analyze or manipulate the data. Use functions like class()
, mode()
, or typeof()
to determine the data type, and functions like as.integer()
, as.vector()
, or as.matrix()
to convert between data types.
Conversion looks like this in code: