Chapter 3 Data Types
3.1 Tutorial Video
Basic Data Types in R
3.2 Reference
Data Types
When programming in R language, you need to use various variables to store various information. Variables are reserved memory locations in PC to store values. When you create a variable you reserve some space in memory.
You may like to store information of various data types like character, integer, (double) floating point, Boolean etc. Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory.
Data Type | Example | You Go |
---|---|---|
Logical | TRUE, FALSE |
x <- TRUE print(class(x)) it produces the following result − [1] "logical" |
Numeric | 112.3, 25, 1999 |
x <- 223.5 print(class(x)) it produces the following result − [1] "numeric" |
Complex | 356 - 52i |
x <- 2+5i print(class(x)) it produces the following result − [1] "complex" |
Character | ‘awesome’ , ‘“good,” “TRUE,”“FALSE,” ’923.4’ |
x <- "TRUE" print(class(x)) it produces the following result − [1] "character" |
R Data Type Exercises (optional)
Exercise 1
Create an atomic vector for Character type, Numeric type, logical type , complex type
Exercise 2
Check the class of each of the data you created in Exercise 1.
Exercise 3 (Dong’s warning: this is a Challenge!)
Using c() function only you learned, is it possible to create a “vector” that contains the heterogeneous data, which must include values/elements in numeric, character, complex and logical data types? if so, can you verify and check what type of vector you created? if not, why is that?
FAQ
3.2.1 Do you need declare varaibles type before you use it in R (RGUI)?
In R, you don’t need to declare the variables as some data types before use it. The variables are assigned with R-Objects and the data type of the R-object becomes the data type of the variable.