Data types in R (Numeric, Character, Logical, Factor)
Data Types in R: Numbers, Characters, and Beyond Data types are a crucial aspect of working with data in R. These categories define the type of information a...
Data Types in R: Numbers, Characters, and Beyond Data types are a crucial aspect of working with data in R. These categories define the type of information a...
Data types are a crucial aspect of working with data in R. These categories define the type of information a variable holds, influencing how it can be processed and analyzed.
Numeric:
Numbers are numerical values represented by digits (whole numbers and decimals). They can be whole numbers (e.g., 1, 2, 3), decimals (e.g., 1.23, 2.45), or even fractions (e.g., 3/4).
Examples:
r
age <- c(25, 30, 35) # numeric
salary <- c(1000, 1200, 1400) # numeric
height <- c(1.7, 1.8, 1.9) # numeric
Character:
Characters are textual values enclosed in quotes. They are used to store words, names, and other textual data.
Examples:
r
name <- c("John", "Mary", "Bob") # character
message <- c("Hello world", "Welcome to R!", "Have a nice day!") # character
Logical:
Logical values represent truth or false statements. They are represented by the values TRUE and FALSE.
Examples:
r
is_adult <- c(TRUE, FALSE, TRUE) # logical
is_positive <- c(TRUE, FALSE, TRUE) # logical
Factor:
Factors are similar to numeric variables but with an additional level of structure. They consist of ordered levels of numeric values.
Examples:
r
region <- c("North America", "South America", "Europe") # factor
grade <- c(1, 2, 3, 4, 5) # factor
gender <- c("Male", "Female", "Male", "Female", "Male") # factor
By understanding these data types, you can ensure your R code handles data appropriately, making it more efficient and reliable in your analysis