R Programming Data Types Notes
R Programming Data Types Notes
Data Types in R
R Tutorial – We shall learn about R atomic data types, different R data types, their syntax and
example R commands for R data types.
While writing a program, you may need to store your data in variables. And this data might be
of different types like Integer, String, Array of Integers etc. Based on these data types, the
Operating System stores them in memory in an optimized manner. Data Types also helpful to
the programmer for understanding the type of data he is handling or manipulating.
Unlike statistically typed languages, R derives the data type of the variable implicitly from the
R object assigned to the variable.
There are many types of R-objects. But all of them are built from R atomic data types. In R
programming language there are six atomic data types.
Following are the six types of vectors that could be built from R atomic data types.
[68, 65, 6C, 6C,6F] is the value for Any data is stored as raw
Raw
string hello. bytes
Note : When data type is Raw, user has to know the format or protocol of the data.
Examples of Atomic Vectors
We shall run the following commands to assign variables, data of different data types and print
the class of the variable to verify the data type.
Logical
> x <- TRUE
> print(class(x))
[1] "logical"
Numeric
> x <- 67.54
> print(class(x))
[1] "numeric"
Integer
x <- 63L
> print(class(x))
[1] "integer"
Complex
> x <- 6 + 4i
> print(class(x))
[1] "complex"
Character
> x <- "hello"
> print(class(x))
[1] "character"
Raw
> x <- charToRaw("hello")
> print(class(x))
[1] "raw"
Data Types of R – Objects
As already mentioned there are many types of R Objects. We shall look into some of the most
commonly used data types. They are :
▪ Vectors
▪ Lists
▪ Matrices
▪ Arrays
▪ Factors
▪ Data Frames
R Vectors
R Lists
[[2]]
[1] "hello"
[[3]]
function (x) .Primitive("tan")
[[4]]
[[4]][[1]]
[1] 8
[[4]][[2]]
[1] "a"
Please observe that fourth and last item in the list is another list.
R Matrices
In R programming language, A Matrix is a 2-D set of data elements. A Vector, number of rows
and number of columns could be used to create a Matrix.
split by row or column : if TRUE then its split by row, else if its FALSE then split by column.
1. Split by row.
> A = matrix(c(1,2,3,4,5,6,7,8),2,4,TRUE) > print(A)
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 5 6 7 8
2. Split by column.
R Arrays
where list contains the elements of array and dimension is a list containing the information
about dimensionality of the array. If dimension is c(2,5,4,8), the array is 4-Dimensional with
dimensions 2x5x4x8.
,,2
R Factors
In R programming language, a Factor is a vector along with the distinct values of vector as
levels. Factors are useful during statistical modelling.
R Data Frames
In R programming language, a Data Frame is a set of equal length vectors. The vectors could
be of different data types.
Conclusion
In this R Tutorial, we have learnt about different R atomic data types and different data types
of R-Objects used most commonly in R programming language.
R Variables
In this tutorial, we shall learn about R Variables, how to assign value to a variable, know the
data type of variable, find the list of variables and delete some of them if required.
Variables are names given to storage locations of data. Once we declare that a variable would
hold some specific data, we may access that specific data in the due course of the program
using the variable name. In R programming language, any R-Object could be stored in a
variable with a name given to the variable.
R Variable can be assigned a value using one of the following three operators :
1. Equal Operator =
2. Leftward Operator <-
3. Rightward Operator ->
In the following, we have examples for equal operator, leftward operator and rightward
operator respectively.
> x = 'hello'
> print(x)
[1] "hello"
> x <- 'learn r' > print(x)
[1] "learn r"
When you are running commands in an R command prompt, the instance might get stacked up
with lot of variables.
To find all R variables that are alive at a point in R command prompt or R script file, ls() is the
command that returns a Character Vector.
> ls()
[1] "a" "A" "dataX"
[4] "factorX" "fruits" "listX"
[7] "RGB" "r programming language" "v"
[10] "values" "x" "y"
> p = 36.89
> ls()
[1] "a" "A" "dataX"
[4] "factorX" "fruits" "listX"
[7] "p" "RGB" "r programming language"
[10] "v" "values" "x"
[13] "y"
Delete an R Variable
> ls()
[1] "a" "A" "dataX"
[4] "factorX" "fruits" "listX"
[7] "p" "RGB" "r programming language"
[10] "v" "values" "x"
[13] "y"
> rm(fruits)
> ls()
[1] "a" "A" "dataX"
[4] "factorX" "listX" "p"
[7] "RGB" "r programming language" "v"
[10] "values" "x" "y"
Conclusion
In this R Tutorial, we have learnt about R Variables and to assign values to them, to find all R
variables and to delete some of them if necessary.
R Operators
There are four main categories of Operators in R programming language. They are shown in
the following picture :
We shall learn about these operators in detail with Example R programs.
R Arithmetic Operators
Arithmetic Operators are used to accomplish arithmetic operations. They can be operated on
the basic data types Numericals, Integers, Complex Numbers. Vectors with these basic data
types can also participate in arithmetic operations, during which the operation is performed on
one to one element basis.
An example for each of the arithmetic operator on Numerical values is provided below.
r_op_arithmetic.R
# R Arithmetic Operators Example for integers
a <- 7.5
b <- 2
Output
$ Rscript r_op_arithmetic.R
[1] 9.5
[1] 5.5
[1] 15
[1] 3.75
[1] 1.5
[1] 3
[1] 56.25
r_op_arithmetic_vector.R
# R Operators - R Arithmetic Operators Example for vectors
a <- c(8, 9, 6)
b <- c(2, 4, 5)
Output
$ Rscript r_op_arithmetic_vector.R
[1] 10 13 11
[1] 6 5 1
[1] 16 36 30
[1] 4.00 2.25 1.20
[1] 0 1 1
[1] 4 2 1
[1] 64 6561 7776
R Relational Operators
Relational Operators are those that find out relation between the two operands provided to
them. Following are the six relational operations R programming language supports.The output
is boolean (TRUE or FALSE) for all of the Relational Operators in R programming language.
An example for each of the relational operator on Numberical values is provided below.
r_op_relational.R
# R Operators - R Relational Operators Example for Numbers
a <- 7.5
b <- 2
Output
$ Rscript r_op_relational.R
[1] FALSE
[1] TRUE
[1] FALSE
[1] FALSE
[1] TRUE
[1] TRUE
r_op_relational_vector.R
# R Operators - R Relational Operators Example for Numbers
a <- c(7.5, 3, 5)
b <- c(2, 7, 0)
Output
$ Rscript r_op_relational_vector.R
[1] FALSE TRUE FALSE
[1] TRUE FALSE TRUE
[1] FALSE FALSE FALSE
[1] FALSE TRUE FALSE
[1] TRUE FALSE TRUE
[1] TRUE TRUE TRUE
R Logical Operators
Logical Operators in R programming language work only for the basic data types logical,
numeric and complex and vectors of these basic data types.
An example for each of the logical operators on Numerical values is provided below.
r_op_logical.R
# R Operators - R Logical Operators Example for basic logical elements
Output
$ Rscript r_op_logical.R
[1] FALSE
[1] TRUE
[1] TRUE
[1] FALSE
[1] TRUE
r_op_logical_vector.R
# R Operators - R Logical Operators Example for boolean vectors
Output
$ Rscript r_op_logical_vector.R
[1] TRUE FALSE FALSE FALSE
[1] TRUE TRUE TRUE FALSE
[1] FALSE FALSE TRUE TRUE
[1] TRUE
[1] TRUE
R Assignment Operators
Assignment Operators are those that help in assigning a value to the variable.
<<- Assigns right side value to left side operand a <<- 3.4
->> Assigns left side value to right side operand c(1,2) ->> a
r_op_assignment.R
# R Operators - R Assignment Operators
a=2
print ( a )
a <- TRUE
print ( a )
454 -> a
print ( a )
a <<- 2.9
print ( a )
c(6, 8, 9) -> a
print ( a )
Output
$ Rscript r_op_assignment.R
[1] 2
[1] TRUE
[1] 454
[1] 2.9
[1] 6 8 9
R Miscellaneous Operators
These operators do not fall into any of the categories mentioned above but are significantly
important during R programming for manipulating data.
Operator Description Usage
A %*%
%*% Performs multiplication of a vector with its transpose
t(A)
r_op_misc.R
# R Operators - R Misc Operators
a = 23:31
print ( a )
M = matrix(c(1,2,3,4), 2, 2, TRUE)
print ( M %*% t(M) )
Output
$ Rscript r_op_misc.R
[1] 23 24 25 26 27 28 29 30 31
[1] TRUE
[,1] [,2]
[1,] 5 11
[2,] 11 25
Conclusion