In R, it is important to know the data type of a variable as it affects how the variable is stored and manipulated. Fortunately, R provides several functions that allow you to determine the data type of a variable.
Using the class() Function
R has a built-in function called class() that returns the class or data type of an object. You can simply pass the variable as an argument to this function to find its data type.
x <- 5 class(x) # Output: "numeric"
The class() function returns "numeric" because the variable x is assigned a numeric value. Similarly, you can use this function with other variables to find their respective data types.
Using the typeof() Function
In addition to the class() function, R also provides another useful function called typeof(). While class() returns the higher-level class of an object (e.g., "numeric", "character", "logical"), typeof() returns the internal storage mode of an object.
y <- "Hello" typeof(y) # Output: "character"
In this example, we assigned a string value to the variable y. The typeof() function correctly identifies its data type as "character".
Distinguishing Between Factors and Characters
In R, factors are used to represent categorical variables. They are different from character strings even though they may appear similar when printed. To differentiate between factors and characters, you can use both factors() and is.character() functions.
z <- factor(c("apple", "banana", "apple", "orange")) class(z) # Output: "factor" is.character(z) # Output: FALSE
The variable z is a factor because it represents a categorical variable. The class() function confirms this by returning "factor". However, when we check if it is a character using the is.character() function, it returns FALSE.
Detecting Missing Values
In R, missing or undefined values are represented by the special value called NA. To identify missing values in a variable, you can use the is.na() function.
w <- c(1, 2, NA, 4) is.na(w) # Output: FALSE FALSE TRUE FALSE
In this example, the variable w contains four elements. The third element is assigned the value of NA, indicating that it is missing. When we apply the is.na() function to this variable, it returns FALSE FALSE TRUE FALSE, indicating which elements are missing.
Finding the data type of a variable in R is essential for understanding how the data will be stored and manipulated. By using the class(), typeof(), and other related functions like factors(),is.character(), and is.na(), you can accurately determine the data type of a variable and handle it accordingly in your R programs.