Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

The First look of R and R Studio

Let's perform some very basic arithmatic function(+)


5+9

## [1] 14

Assign the value 6 to a variable "a"


a <- 6

If you assign another value to this variable, it will overwrite the existing value. For example
let's assign value 5 to the variable "a"
a <- 5

You can assign values to variables and then perform arithmatic functions on that. See the
example below:
a <- 7
b <- 77
a + b

## [1] 84

Introducing Functions
Print Function
Lets say if we assign the number 2 to a variable and then you can use the below command
to print "a".
a <- 2
print(a)

## [1] 2

Functions have arguments. In the above case the function print has an argument. telling R
to print the variable "a"
Typically we provide the name of the function and in the bracket we provide arguments.
Function(arguments)
Point to Remember:
R is case sensitive. This means you need to type the function name in the specific case (lower case or upper case).
For example the function mean can not be typed as Mean. But good news is that R Studio shows the options as
you type.
Vectors
We will cover vectors later in this course. At this stage, please understand that you can
assign multiple values to a variable. In the below example the height of multiple students
have been stotred in variable students.height
student.height <- c(101, 122, 115, 117, 103)

To view what is stored in student.height, you can just type the name of the variable, and it
will return the set of values stored in it.
student.height

## [1] 101 122 115 117 103

If you need to find the mean of these 5 heights stored in student.height, you can use the
function mean and provide the name of the variable as the argument.
mean(student.height)

## [1] 111.6

This was the first introduction to R functions. We will learn more functions in further
lectures.

Saving Files
You can save R script file with .R extension.
If you save the file using R command, it will be saved in the working directory. However
you can save the file in any directory you ant using the pull down menu.
Two commands you might want to check in this topic are getwd() and setwd().
You can check the working directory.
getwd()

## [1] "C:/Users/HOME-PC/Documents/R/R Markdowns"

You can set the working directory using the command setwd() and providing the path.
setwd("C:/Users/")

Data Types in R
Following are the key data types in R:
• Numeric - (This include: 1. integers and 2. decimal numbers called floating point type)
• Characters - For example "Sandeep Kumar" or 'Sandeep Kumar'

• Logical - TRUE/FALSE (or T/F)

• Complex - e.g. (2+3i) - we will not cover this data type in this course.

You can check the type using the class() function:


a <- 2
class(a)

## [1] "numeric"

b<- 2.2
class(b)

## [1] "numeric"

c <- "Sandeep Kumar"


class(c)

## [1] "character"

d <- TRUE
class(d)

## [1] "logical"

e <- F
class(e)

## [1] "logical"
Point to Remember:
You can repeat the last command in the Console using the up arrow. By pressing up key multiple times you can
move to previously used commands.

Simple Mathematical Operations


Basic mathematical functions
Plus
2 + 2

## [1] 4

Minus
9 - 5

## [1] 4
Multiplication
5 * 2

## [1] 10

Division
5 / 2

## [1] 2.5

Power : 5 to the power 2 (or 5 square)


5 ^ 2

## [1] 25

Remainder: When you devide 5 by 2, you get 1 as the remainder


5 %% 2

## [1] 1

Quotient: In the above example, you get 2 as the whole number as the result of division.
5 %/% 2

## [1] 2

You might also like