R Programming language - R Data Types
In R, there are several data types that are used to store different kinds of data values. Understanding the different data types in R is essential to effectively working with data and writing R code. Here are some of the most commonly used data types in R:
- Numeric: Numeric data types are used to store numeric values, including integers and real numbers.
x <- 5 # integer value y <- 3.14 # real number value
- Character: Character data types are used to store text values, such as words and sentences.
x <- "Hello" # character string value y <- 'world' # another character string value
- Logical: Logical data types are used to store Boolean values, which can be either TRUE or FALSE.
x <- TRUE # logical TRUE value y <- FALSE # logical FALSE value
- Factor: Factor data types are used to store categorical data, where the possible values are predefined categories.
x <- factor(c("red", "green", "blue")) # create a factor with three levels
- Date and Time: Date and Time data types are used to store dates and times.
x <- as.Date("2022-03-01") # create a date value y <- as.POSIXct("2022-03-01 12:00:00") # create a time value
- List: List data types are used to store multiple values of different data types in a single object.
x <- list("apple", 5, TRUE) # create a list with three values of different data types
In addition to these data types, R also has other specialized data types, such as matrices, arrays, and data frames, that are used for more complex data structures.
In summary, R has several data types that are used to store different kinds of data values, including numeric, character, logical, factor, date and time, and list data types. Understanding these data types is essential to effectively working with data in R.