Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Basics of R Programming - Part 2

basics of R explained beautifully for everyone's understanding and practice for learning and

Uploaded by

Amit Chowdhury
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Basics of R Programming - Part 2

basics of R explained beautifully for everyone's understanding and practice for learning and

Uploaded by

Amit Chowdhury
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Subject: R Programming

Topic: Basics of R Programming (Contd…)


Faculty: Sabyasachi Mukhopadhyay

Basic Operations Using R


Arithmetic with R
In its most basic form, R can be used as a simple calculator. Consider the following arithmetic
operators:
● Addition: +
● Subtraction: -
● Multiplication: *
● Division: /
● Exponentiation: ^
● Modulo: %%
The last two might need some explanations.
● The ^ operator raises the number to its left to the power of the number to its right: for
example 3^2 is 9.
● The modulo returns the remainder of the division of the number to the left by the number
on its right, for example 5 modulo 3 or 5 %% 3 is 2.
Codes & Outputs
> # An addition
>5+5
[1] 10
> # A subtraction
>5-5
[1] 0
> # A multiplication
>3*5
[1] 15
> # A division
> (5 + 5) / 2
[1] 5
> #Exponentiation
>2^5
[1] 32
> # Modulo
> 28 %% 6
[1] 4
Selection by comparison
By making use of comparison operators, we can approach the previous question in a more
proactive way.
The (logical) comparison operators known to R are:
< for less than
> for greater than
<= for less than or equal to
>= for greater than or equal to
== for equal to each other
!= not equal to each other
Hence, stating 6 < 5 returns FALSE.
Codes & Outputs
> 6<5
[1] FALSE
> 8 != 8
[1] FALSE
The great thing about R is that we can utilize these comparison operators also on vectors. Such
as,
Code & Output
> c(4, 5, 6) > 5
[1] FALSE FALSE TRUE
Let’s look at another interesting example.
Code & Output
> x <- c('A', 4, 5) > 5
>x
[1] TRUE FALSE FALSE

Here, 65 (ASCII value of character ‘A’) is compared with 5 and that’s why the output is TRUE.

Variables: Variables are nothing but reserved memory locations to store values. This implies that
when you create a variable you reserve some space in memory. Such as: B= True, x = 15 are the
variables occupying the memory locations.
A basic concept in (statistical) programming is called a variable. A variable allows you to store a
value (e.g. 42) or an object (e.g. a function description) in R. You can then later use this
variable's name to easily access the value or the object that is stored within this variable. You can
assign a value 42 to a variable x with the command.
Code & Output
# Assign the value 42 to x
> x <- 42
> # Print out the value of the variable x
>x
[1] 42
Suppose you have a fruit basket with five apples. As a data analyst in training, you want to store
the number of apples in a variable with the name my_apples.
Code & Output
> # Assign the value 5 to the variable my_apples
> my_apples <- 5
> # Print out the value of the variable my_apples
> my_apples
[1] 5

Every tasty fruit basket needs oranges, so you decide to add six oranges. As a data analyst, your
reflex is to immediately create the variable my_oranges and assign the value 6 to it.
Code & Output
> # Assign the value 6 to the variable my_oranges
> my_oranges <- 6
> # Print out the value of the variable my_oranges
> my_oranges
[1] 6

Next, you want to calculate how many pieces of fruit you have in total. Since you have given
meaningful names to these values, you can now code this in a clear way:
Code & Output
> # Add these two variables together
> my_apples + my_oranges
[1] 11
Here you can create a variable my_fruit and store the result there.
Code & Output
> # Create the variable my_fruit
> my_fruit = my_apples + my_oranges
> my_fruit
[1] 11

There are several R-objects, for instance, vectors, matrices, arrays, data frames, lists, factors. In
R, the basic data types like logical, numeric, character etc. come under vectors. These basic data
types are referred to as ‘Atomic Vectors’. There are six basic data types, such as, logical,
numeric, integer, complex, character and raw. We shall gradually explore by following examples.
In the following example, we shall come across a special type of function, combining function
c() .
Codes & Outputs
> #vectors
> #Logical
> vtr1 = c(TRUE, FALSE)
> class(vtr1)
[1] "logical"
> #Numeric
> vtr2 = c(15, 85.6, 9999)
> class(vtr2)
[1] "numeric"
> #Integer
> vtr3 = c(35L, 58L, 146L)
> class(vtr3)
[1] "integer"
> #Complex
> vtr4 = c(4+3i, 8+7i)
> class(vtr4)
[1] "complex"
> #Character
> vtr5 = c("A")
> class(vtr5)
[1] "character"
> #Raw
> vtr6 = charToRaw("X")
> class(vtr6)
[1] "raw"

Now, if a vector has a collection of different data types, they will be converted into a common
data type like numerical by considering ‘TRUE’ as 1 in the following example.
Code & Output
> vtr = c(TRUE, 35L, 3.14)
> class(vtr)
[1]"numeric"
While dealing with combinations of characters, logical, and integers; all entries will be converted
into characters.
Code & Output
> v = c("hello", "FALSE", 65L)
> class(v)
[1] "character"
There is a function like typeof() in R to check the data types. Let’s discuss some following
examples.

Codes & Outputs


> typeof("Data Science")
[1] "character"
> typeof(13L)
[1] "integer"
Although there are similarities between typeof() and class() functions, typeof() function is more
focused on type of data storage whereas class() function is more focused on object oriented
classifications. In the aspect of data storage, there is another kind of data type, named ‘Double’
(for double precision floating point numbers). For instance,
Code & Output
> typeof(99.57)
[1] "double"
Now, let’s discuss the R objects.
Vectors
A one dimensional array is called a Vector. Such as,
Code & Output
> # Create a vector
> fruits <- c('pineapple','orange','mango')
> fruits
[1] "pineapple" "orange" "mango"
Matrices
A two dimensional rectangular dataset is referred to as ‘Matrix’. Such as,
Code & Output
> # Create a matrix
> m = matrix( c('a','a','b','c','b','c'), nrow = 2, ncol = 3, byrow = TRUE)
> print(m)
[,1] [,2] [,3]
[1,] "a" "a" "b"
[2,] "c" "b" "c"
Here, byrow = TRUE implies row-wise entry in a matrix.
Arrays
There is no restriction of dimension in an array, and that’s the major difference between matrix
and array. Let's explore an array with the following example.
Code & Output
> x <- array(c('red','green'),dim = c(3,3,2))
>x
,,1
[,1] [,2] [,3]
[1,] "red" "green" "red"
[2,] "green" "red" "green"
[3,] "red" "green" "red"
,,2
[,1] [,2] [,3]
[1,] "green" "red" "green"
[2,] "red" "green" "red"
[3,] "green" "red" "green"
Lists
Lists in an R-object can have different types of elements like vectors, functions and even another
list within it. For example,
Code & Output
> list1 <- list(c(4,2,8),5.9,sin)
> print(list1)
[[1]]
[1] 4 2 8
[[2]]
[1] 5.9
[[3]]
function (x) .Primitive("sin")
Factors
Factors can be created using vectors and referred to as labels. These labels can be characters and
will not be dependent upon the pattern of input vectors like characters, Booleans or numeric.
Factors are useful for statistical modeling and can be created using factor() function where
nlevels function counts the number of functions. For example,
Code & Output
> # Create a vector
> colors <- c('red','red','green','violet','green','green','violet')
> # Create a factor object
> factor_colors <- factor(colors)
> # Print the factor
> print(factor_colors)
[1] red red green violet green green violet
Levels: green red violet
> print(nlevels(factor_colors))
[1] 3
Data Frames
The list of equal length vectors, where each column consists of different types of data in order to
form tabular data objects, is known as Data Frames.
Data frames can be generated using the data.frame() function. For example,
Code & Output
> vtr <- data.frame(
+ name = c("Naina", "Subham","Dipti"),
+ job = c("Clerk", "Physicist", "Actor"),
+ salary = c("Low", "Medium", "High")
+)
>print(vtr)
name job salary
1 Naina Clerk Low
2 Subham Physicist Medium
3 Dipti Actor High

You might also like