0% found this document useful (0 votes)
17 views

R Programming Data Types Notes

This document provides an overview of data types in R, including atomic data types such as Logical, Numeric, Integer, Complex, Character, and Raw, as well as R objects like Vectors, Lists, Matrices, Arrays, Factors, and Data Frames. It explains how to define and manipulate these data types and introduces R variables, their assignment, and management. Additionally, it covers R operators, including Arithmetic, Relational, and Logical operators, with examples for each.

Uploaded by

arjun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

R Programming Data Types Notes

This document provides an overview of data types in R, including atomic data types such as Logical, Numeric, Integer, Complex, Character, and Raw, as well as R objects like Vectors, Lists, Matrices, Arrays, Factors, and Data Frames. It explains how to define and manipulate these data types and introduces R variables, their assignment, and management. Additionally, it covers R operators, including Arithmetic, Relational, and Logical operators, with examples for each.

Uploaded by

arjun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

R Data Types

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.

Atomic Vectors of R Atomic Data Types

Following are the six types of vectors that could be built from R atomic data types.

Data Type Example Description

Logical TRUE, FALSE boolean values

Numeric 2, 45.9, 3782 Numbers of all kinds

Integer 9L, 779L Explicitly Integers

Real Value + Complex


Complex 8+9i
Value

Character ‘m’, “hello” Characters and Strings

[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

We shall in detail about about these data types.

R Vectors

In R programming language, a Vector is a fixed-length collection of values of a data type. The


vector would get the data type of items in the collection.

Syntax – Define a Vector


variable <- c(comma separated atomic vectors belonging to a data type)

For example (‘apple’,’orange’,”banana”) is a vector and is a collection of values of data type


Character. So the vector would become a Character Vector. Similarly an Integer Vector or
Complex Vector.

Following is an example of a Character Vector. We shall learn how to assign an R Character


Vector to a variable, print the vector and verify the data type of vector.

> fruits = c('apple','orange',"banana") > print(class(fruits))


[1] "character"
> print(fruits)
[1] "apple" "orange" "banana"

R Lists

In R programming language, a List is a collection of List Items (R Objects) belonging to


different data types. A List may contain another list as its item. A List Item may contain a
Matrices, an Array, a Factor, an R function or any of R Object.

Syntax to Define List


variable <- list(comma seperated list items)
Following is an example of an R List. We shall learn how to assign a list of Number, Character,
Function and another list to a List and print the List.

> listX = list(51,"hello",tan,list(8L,"a")) > print(listX)


[[1]]
[1] 51

[[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.

Syntax – Define Matrix


variable <- matrix(vector, number of rows, number of columns, split by row or column)

split by row or column : if TRUE then its split by row, else if its FALSE then split by column.

Following is an example to define a matrix :

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.

> A <- matrix(c(1,2,3,4,5,6,7,8),2,4,FALSE) > print(A)


[,1] [,2] [,3] [,4]
[1,] 1 3 5 7
[2,] 2 4 6 8

R Arrays

In R programming language, Arrays are N-Dimensional data sets.

Syntax – Define an R Array


variable <- array(list, dimension)

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.

Following is an example of 3-D array.

> A = array(c(1,2,3,4,5,6,7,8,9,10,11,12),c(2,3,2)) > print(A)


,,1

[,1] [,2] [,3]


[1,] 1 3 5
[2,] 2 4 6

,,2

[,1] [,2] [,3]


[1,] 7 9 11
[2,] 8 10 12

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.

Levels are stored as R Characters.

Syntax – Define an R Factor


variable <- factor(vector)

Following is an example to define an R Factor

> factorX = factor(c(1,4,7,2,6,7,1,6,4)) > print(factorX)


[1] 1 4 7 2 6 7 1 6 4
Levels: 1 2 4 6 7

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.

Syntax – Define an R Data Frame


variable <- data.frame(listA, listB, listC, .., listN)

Following is an example to define an R Data Frame :

> dataX = data.frame(values = c(21,42,113), RGB = c('red','blue','green')) > print(dataX)


values RGB
1 21 red
2 42 blue
3 113 green

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.

Rules for writing R Variables

Following are the rules to give a valid name to an R variable.

▪ It may contain letters (Examples: x, y, varx, .. )


▪ It may contain numbers (Examples: x1, y1, var25x, .. )
▪ It may contain special char Dot (.) (Examples: x., y., var.x, .. )
▪ It may contain special char Underscore(_) (Examples : x_1, y_2, var_x, .. )
▪ The first character in the name could be a letter. (Examples : x, y, varx, .. )
▪ The first character in the name could be a dot not followed by a
number. (Examples : .x, .y, .varx, .. )
▪ Reserved words in R could not be used for variables.

Examples for invalid variable names : .2x, tan, er@t

Assign value to R 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"

> 'r programming language' -> x


> print(x)
[1] "r programming language"

Find all R Variables

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

rm(variable_name) deletes an R variable from the stack of variables in a running instance.

> 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 – Arithmetic, Relational, Logical, Assignment

R Tutorial – We shall learn about R Operators – Arithmetic, Relational, Logical, Assignment


and some of the Miscellaneous Operators that R programming language provides.

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.

Operator Description Usage

+ Addition of two operands a+b

– Subtraction of second operand from first a–b

* Multiplication of two operands a*b


Operator Description Usage

/ Division of first operand with second a/b

%% Remainder from division of first operand with second a %% b

%/% Quotient from division of first operand with second a %/% b

^ First operand raised to the power of second operand a^b

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

print ( a+b ) #addition


print ( a-b ) #subtraction
print ( a*b ) #multiplication
print ( a/b ) #Division
print ( a%%b ) #Reminder
print ( a%/%b ) #Quotient
print ( a^b ) #Power of

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

An example for each of the arithmetic operator on Vectors is provided below.

r_op_arithmetic_vector.R
# R Operators - R Arithmetic Operators Example for vectors

a <- c(8, 9, 6)
b <- c(2, 4, 5)

print ( a+b ) #addition


print ( a-b ) #subtraction
print ( a*b ) #multiplication
print ( a/b ) #Division
print ( a%%b ) #Reminder
print ( a%/%b ) #Quotient
print ( a^b ) #Power of

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.

Operator Description Usage

< Is first operand less than second operand a<b

> Is first operand greater than second operand a>b

== Is first operand equal to second operand a == b

<= Is first operand less than or equal to second operand a <= b

>= Is first operand greater than or equal to second operand a>=b

!= Is first operand not equal to second operand a!=b

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

print ( ab ) # greater than


print ( a==b ) # equal to
print ( a<=b ) # less than or equal to
print ( a>=b ) # greater than or equal to
print ( a!=b ) # not equal to

Output
$ Rscript r_op_relational.R
[1] FALSE
[1] TRUE
[1] FALSE
[1] FALSE
[1] TRUE
[1] TRUE

An example for each of the relational operator on Vectors is provided below.

r_op_relational_vector.R
# R Operators - R Relational Operators Example for Numbers

a <- c(7.5, 3, 5)
b <- c(2, 7, 0)

print ( a<b ) # less than


print ( a>b ) # greater than
print ( a==b ) # equal to
print ( a<=b ) # less than or equal to
print ( a>=b ) # greater than or equal to
print ( a!=b ) # not equal to

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.

Operator Description Usage

& Element wise logical AND operation. a&b

| Element wise logical OR operation. a|b

! Element wise logical NOT operation. !a

&& Operand wise logical AND operation. a && b

|| Operand wise logical OR operation. a || b

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

a <- 0 # logical FALSE


b <- 2 # logical TRUE
print ( a & b ) # logical AND element wise
print ( a | b ) # logical OR element wise
print ( !a ) # logical NOT element wise
print ( a && b ) # logical AND consolidated for all elements
print ( a || b ) # logical OR consolidated for all elements

Output
$ Rscript r_op_logical.R
[1] FALSE
[1] TRUE
[1] TRUE
[1] FALSE
[1] TRUE

An example for each of the logical operators on Vectors is provided below.

r_op_logical_vector.R
# R Operators - R Logical Operators Example for boolean vectors

a <- c(TRUE, TRUE, FALSE, FALSE)


b <- c(TRUE, FALSE, TRUE, FALSE)

print ( a & b ) # logical AND element wise


print ( a | b ) # logical OR element wise
print ( !a ) # logical NOT element wise
print ( a && b ) # logical AND consolidated for all elements
print ( a || b ) # logical OR consolidated for all elements

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.

Operator Description Usage

= Assigns right side value to left side operand a=3

<- Assigns right side value to left side operand a <- 5

-> Assigns left side value to right side operand 4 -> a

<<- Assigns right side value to left side operand a <<- 3.4

->> Assigns left side value to right side operand c(1,2) ->> a

An example for each of the assignment operators is provided below.

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

Creates series of numbers from left operand to right


: a:b
operand

%in% Identifies if an element(a) belongs to a vector(b) a %in% b

A %*%
%*% Performs multiplication of a vector with its transpose
t(A)

An example for each of the Miscellaneous operators is provided below.

r_op_misc.R
# R Operators - R Misc Operators

a = 23:31
print ( a )

a = c(25, 27, 76)


b = 27
print ( b %in% 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

In this R Tutorial, we have learnt about R Operators – R Arithmetic Operators, R Relational


Operators, R Logical Operators, R Assignment Operators, R Miscellaneous Operators with
example R commands and R script files.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy