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

R Introduction II

This document provides an introduction and overview of R and RStudio. It begins with the objectives of understanding R as a programming language, installing R and RStudio, and an overview of RStudio. It then demonstrates basic R functions for arithmetic, trigonometry, vectors, matrices, and data frames. Examples are provided for various data types and operations in R including addition, division, logarithms, and assigning objects. The document concludes with a brief discussion of importing data into R and creating R projects.

Uploaded by

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

R Introduction II

This document provides an introduction and overview of R and RStudio. It begins with the objectives of understanding R as a programming language, installing R and RStudio, and an overview of RStudio. It then demonstrates basic R functions for arithmetic, trigonometry, vectors, matrices, and data frames. Examples are provided for various data types and operations in R including addition, division, logarithms, and assigning objects. The document concludes with a brief discussion of importing data into R and creating R projects.

Uploaded by

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

Introduction to R

and Rstudio
PhD: HUAMAN DE LA CRUZ, Alex Ruben
Email contact: [email protected]
OBJECTIVES

1. Understand R as a programming language


2. Install R and R Studio
3. Overview of R Studio
4. Introduction of R as an object oriented
language
Software Environment

1. Statistics
2. Graphics
3. Programming
4. Calculator
5. GIS
6. Etc……….
R Basic examples - Arithmetic
# Addition
1 + 1
[1] 2
# Multiplication
10 * 10
[1] 100
# Compute Logarithm
log10(100)
[1] 2
# 7 to the 8th power
7^8
[1] 5764801
# square root of 2
sqrt(2)
[1] 1.414214
Notice the comment character #.
R Basic examples - Trigonometry

#Sine(π/2):
sin(pi/2) [1] 1
#Cosine(π):
cos(pi) [1] -1
#Tangent(π/4):
tan(pi/3) [1] 1.732051
#Cotangent(π/3):
1/tan(pi/3) [1] 0.5773503
#cosine inverse = -1
acos(-1) [1] 3.141593
#tangent inverse = 0.5
atan(0.5) [1] 0.4636476
R Basic examples
# Division
10/3
[1] 3.333333
# Square root
Square(2)
[1] 2.718281

Options (digits=3)

# Division
10/3
[1] 3.33
# Square root
Sqrt(2)
[1] 1.41
R Basic examples

# Print Text
"Hello World"
[1] “Hello World”
R Assignment, Object names, and
Data types
# Assignment
test <- 1
#or
test = 1
# dont see the calculated value
test #take a look
[1] 1

10 -> x # Rightwards assignment


x
[1] 10
AS VARIABLES and Object names

x <- 7*41/pi
x numeric
[1] 91.35494

a = "The dog ate my homework"


sub("dog","cat",a)
character
[1] "The cat ate my homework“ string
b<-c("the", "dog","ate", "my","homework")
b
[1] “the” “dog” “ate”
[4] “my” “homework”
AS VARIABLES and object names

s = (1+1==3)
s
[1] FALSE

p = (1+1+1==3)
s
[1] TRUE

logical
AS VARIABLES and object names

x <- 5
y <- 16
x<y
[1] TRUE
x>y
[1] FALSE
x<=5
[1] TRUE
y>=20
[1] FALSE
y == 16
[1] TRUE
x != 5
[1] FALSE
Grouped Expressions in R

You can issue many commands on a


single line by separating each command
with a semicolon (;). When doing so,
evaluation of commands is in order from
left to right:

A <- 1; B <- 2; C <- A + B


C
[1] 3
Data Types
VECTORS

In R, Vector is a basic structure that


contains element of similar type
(logical, integer, double, character,
complex or raw
R <- c(2, 3, 5)
R
[1] 2 3 5
lenght (R)
[1] 3
T<- length(c("aa", "bb", "cc", "dd", "ee")
T
[1] 5
VECTORS - Exercises
a<-c(8.5:4.5)#sequence of numbers from 8.5 down to 4.5
b<-c(20:1)
c<-c(1:20)
d<-c(1, 1:3, c(5, 8), 13)#values concatenated into single value
length(a)
e<- c(8:20, step=2)
seq(0,1, length=11)
str(seq(rnorm(20)))
seq(1,9, by = 2) # match
seq(1,9, by = pi)# stay below
seq(1,6, by = 3)
seq(1.575, 5.125, by=0.05)
f <-seq(2,20, by=2)
VECTORS
Exercises # MINIMUM AND MAXIMUM VALUE
i <- c(1,-2,3,-4)
j <- c(-1,2,-3,4)
min(i)
min(j)
min(i,j)
max(i)
max(j)
max(i,j)
pmin(i,j) # show all minor values
pmax(i,j)
## lenght
length(i)
length(j)
mean(i)
mean(j)
var(i)
var(j)
sd(i)
sd(j)
VECTORS - Exercises
TEMPERATURE<-c(1,5,6,9)
TEMPERATURE<-c(2:5)
TEMPERATURE
TEMPERATURE[2] # show the 2 element
[1] 5
TEMPERATURE[c(1,3,4)] #Select specific set
of elements
TEMPERATURE[3:4]
TEMPERATURE[-2]
#This will give a vector of all elements except 2 nd element.

[1] 2 4 5
LISTS IN R

A list is a generic vector containing


other objects
n<-c(2,3,5)
s<-c("a","b","c","d","e")
b<-c(TRUE,FALSE,TRUE,FALSE,FALSE)
x<-list(n,s,b,3) # x contains copies of n,s,b
x[2] # slice containing the second member of x
x[1]
x[c(2,4)] #retrieve a slice containing multiple
members
LISTS IN R

A list is a generic vector containing


other objects

# COPY}
x[[2]]
# we can modify its content directly
x[[2]][1]="ta"
x[[2]]
s # is not affected
MATRICES IN R

Matrix is a collection of data elements


arranged in a two-dimensional
rectangular layout. The following is an
example of a matrix with 2 rows and 3
columns. matrix () function
MATRICES IN R

A<-matrix(c(2,4,3,1,5,7), nrow = 2,
ncol = 3)

A<-matrix(c(2,4,3,1,5,7), nrow = 2,
ncol = 3, byrow = TRUE)
MATRICES IN R
MATRICES IN R
Matrices can represent the binding of two or more vectors of equal
length. If we have the X and Y coordinates for five quadrats within a grid,
we can use cbind() (combine by columns) or rbind() (combine by rows)
functions to combine them into a single matrix, as follows:
X <- c(16.92, 24.03, 7.61, 15.49, 11.77)
Y <- c(8.37, 12.93, 16.65, 12.2, 13.12) A<-c(16.92, 8.37)
XY <- cbind(X, Y) B<-c(24.03, 12.93)
XY <- rbind(X,Y) C<-c(7.61, 16.65)
#Exercises D<-c(15.42, 12.20)
E<-c(11.77, 13.12)
XY<-cbind(A,B,C,D,E)
XY
XY[2,3] # [row, column]
XY[2,] #It displays entire second row
XY[,2] #It displays entire second column
XY[,-2] #It displays all columns except second column
XY[1:2, "A"] #It displays rows 1 trough 2 for column A
XY[,"C"] #It displays column named C
DATAFRAME IN R

A dataframe is used for storing data tables. It is a


list of vectors of equal length, data.frame () function

# Dataframe
edad <- c(22, 34, 29, 25, 30, 33, 31, 27, 25, 25)
tiempo <- c(14.21, 10.36, 11.89, 13.81, 12.03, 10.99,
12.48, 13.37, 12.29, 11.92)
sexo <- c("M","H","H","M","M","H","M","M","H","H")
misDatos <- data.frame(edad,tiempo,sexo)
misDatos
DATAFRAME IN R

A dataframe is used for storing data tables. It is a


list of vectors of equal length, data.frame () function

# Dataframe
edad <- c(22, 34, 29, 25, 30, 33, 31, 27, 25, 25)
tiempo <- c(14.21, 10.36, 11.89, 13.81, 12.03, 10.99,
12.48, 13.37, 12.29, 11.92)
sexo <- c("M","H","H","M","M","H","M","M","H","H")
misDatos <- data.frame(edad,tiempo,sexo)
misDatos
DATAFRAME IN R

A dataframe is used for storing data tables. It is a


list of vectors of equal length, data.frame () function

# working with my data


str(misDatos) # Structure of 'misDatos'
names(misDatos)# Name of all variables contained 'misDatos'
misDatos[3:6,] #accesing to my data
misDatos[,1]
misDatos$edad
misDatos[,"edad"]
misDatos[["edad"]]
# mean of several forms
mean(misDatos[,1])
mean(misDatos$edad)
mean(misDatos[,"edad"])
mean(misDatos[["edad"]])
DATAFRAME IN R

A dataframe is used for storing data tables. It is a


list of vectors of equal length, data.frame () function

# working with my data


str(misDatos) # Structure of 'misDatos'
names(misDatos)# Name of all variables contained 'misDatos'
misDatos[3:6,] #accesing to my data
misDatos[,1]
misDatos$edad
misDatos[,"edad"]
misDatos[["edad"]]
# mean of several forms
mean(misDatos[,1])
mean(misDatos$edad)
mean(misDatos[,"edad"])
mean(misDatos[["edad"]])
attach(misDatos) table(estudios) table(sexo) table(edad) table(sexo,edad) mean(edad[sexo=="M"]) mean(edad[sexo=="H"]) de

DATAFRAME IN R

A dataframe is used for storing data tables. It is a


list of vectors of equal length, data.frame () function

# to know the quantity


table(misDatos$sexo)
table(misDatos$edad)
table(misDatos$tiempo)
mean(misDatos$edad[misDatos$sexo=="M"])
mean(misDatos$edad[misDatos$sexo=="H"])
#attach (enganchar) and detach (desenganchar)
attach(misDatos)
able(estudios)
table(sexo)
table(edad)
table(sexo,edad)
mean(edad[sexo=="M"])
mean(edad[sexo=="H"])
detach(misDatos)
DATAFRAME IN R

A dataframe is used for storing data tables. It is a


list of vectors of equal length, data.frame () function

# Creamos el dataframe 'divisas' con la tasa de cambio de algunas


monedas
divisas = data.frame(moneda=c("Libra", "Euro", "Rublo"), cambio=c(1.2,
1, 0.02))
divisas
# Creamos un dataframe con el nombre de algunos paises y su moneda
nacional
paises = data.frame(pais=c("EEUU", "Venezuela", "Japón"),
moneda=c("Dólar", "Bolívar", "Yen"))
paises
Importing Data in R Studio

✓Installation of packages
✓Importing different data into r
✓Data manipulation in R
NEXT
Open new Script
Importing different data into R
Creating a new Project
Importing different data into R
Creating a new Project
Importing different data into R

•Open your Excel data


•Go to File > Save As or press Ctrl+Shift+S
•Name this with anything you want, say Data. Then
before clicking Save, make sure to change the File
Format to Comma Delimited Text and better set the
directory to My Documents folder, for Windows.
•When saved, this file will have a name Data.csv.
Importing different data into R

# Dataframe
edad <- c(22, 34, 29, 25, 30, 33, 31, 27, 25, 25)
tiempo <- c(14.21, 10.36, 11.89, 13.81, 12.03, 10.99,
12.48, 13.37, 12.29, 11.92)
sexo <- c("M","H","H","M","M","H","M","M","H","H")
misDatos <- data.frame(edad,tiempo,sexo)
misDatos
Importing different data into R
Importing different data into R
Importing different data into R
Importing different data into R
Importing different data into R
Importing different data into R
Importing different data into R
Importing different data into R

•NEXT SOON
•Importing Data from URL (web)
•ANOVA
•FACTORIAL DESIGN
THANKS FOR ALL

You might also like