Overview:
1. Integer – whole number.
2. Numeric – numbers with or without decimal values.
3. Logical – TRUE or FALSE.
4. Complex – contains imaginary values.
5. Character – letters, symbols, and number – to be wrapped within single or double inverted commas.
In the last blog post, I introduced how to create a variable. As you know, a variable created in an R environment can hold different kinds of data. There are five types of data in R: integer, numeric, logical, complex, and character. As a data analyst, knowing the data type is significant – as it decides the statistical analysis you wish to choose.
In R it is easy to find the data type with the help of class function. A user can also use typeof function to identify data class.
Integer:
An integer is a whole number without a fraction. For example, 3 is an integer, and 3.14 is not. Let’s create a variable X and store the integer 16 into it.
#Copy and run this code in your R Console:
# Creating variable X with value 16:
X <- 16
# Checking the class of the variable X
class(X)
# When you run class(X), it will show that it is a numerical data instead of integer
# Generally, a number with or without fraction is identified as numerical data
# To declare a value as integer, you need to add L at the end of the number
X <- 16L
class(X)
# This is an integer!
Numeric:
A numerical data is a value with decimal values. For example, 83.7 is numerical data.
# Creating numerical data:
product_price <- 34.50
class(product_price)
# Creating gre_score variable with value 320
gre_score <- 320
class(gre_score) # technically this is suppose to be an integer. However, R identifies it as numerical data.
Logical:
A logical data contains only two values TRUE or FALSE – and is also called Boolean in R. A user can use T instead of TRUE and F instead of FALSE.
# Creating logical data:
log_value_1 <- TRUE
log_value_2 <- FALSE
class(log_value_1)
class(log_value_2)
Complex:
R can help a user to store any imaginary component that represents a complex number.
# Creating variable 'comp_data' with complex number
comp_data <- 10 + 2i
class(comp_data) # the result will be "complex"
Character:
A character data represents alphabets, symbols, or numbers. It is also called string data. To represent character data in R, a user must wrap the alphabets within single or double inverted commas.
# Let's create a variable 'my_name' with my name
my_name <- "Balachandar"
class(my_name) # the result will be "character"
# A user can also declare a number as character
my_age <- "23"
my_dob <- "01.01.2011"
class(my_age)
class(my_dob)
In R, there are also raw data. It represents the raw bytes of a value. I ignored writing about it since I do not use them for my data analysis.
Leave a Reply