Introduction to Basics of R: Getting Started in R and RStudio

Start your Base R on your laptop or computer

Basically, R and Rstudio are the same software, however RStudio is an Integrated Development Environment (IDE) wherein you can run, edit

commands and see the results simaltanouesly. Therefore whatever commands that you run in Base R and RStudio will give the same results. Quickly

run some mathematical operations in Base R and move to RStudio

# is used for adding comments

# <- is considered as equal to sign in R

#Let us run command

print("Welcome to R!")

## print() is a function. it tells computer to do something

## “Welcome to R!” is an argument

the information function needs to run to give output

# Perform basic mathematical operation
Add
12+6
substract
12-6
#multiplying 12 by 6
12*6
#divide 12 by 6
12/6

# In Similar fashion you can do all the mathematical calculations.

Variable defination in R

# Variable defination in R # We are assigning value 3 to variable x and value 5 to variable y. Print the value of x and y
x <- 3
y <- 5

x

y

z <- x + y
z
n <- z

# assigning value to my_var
my_var <- 42
my_var
my_banana <- 6
my_banana
my_apple <- 5
my_apple
my_chikoo <- 5
my_chikoo
my_pin <- 3
my_pin

# adding fruits
my_basket <- my_banana + my_apple + my_chikoo + my_pin
my_basket
avg_fruit <- (my_banana + my_apple + my_chikoo + my_pin)/4
avg_fruit

Basic data types in R

## Numerics ## Character Text ## Boolean values

## Class of the data
> x <- 5.5
> x
[1] 5.5
> y <- 5
>
> z<- 3 + 2i
> z
[1] 3+2i
> ## # The quotation marks indicate that the variable is of type character
> my_character <- " universe"
> my_character
[1] " universe"
> class(x)
[1] "numeric"
> class(y)
[1] "numeric"
> class(z)
[1] "complex"
> class( my_character)
[1] "character"
>
> my_character <- "1"
> my_logical <- TRUE
> my_logical <- FALSE
> ## What is the class of this variable?
>
> my_character42 <- "forty-two"
> class(my_character42)
[1] "character"

Leave a Reply

Your email address will not be published. Required fields are marked *