R Introduction II
R Introduction II
and Rstudio
PhD: HUAMAN DE LA CRUZ, Alex Ruben
Email contact: [email protected]
OBJECTIVES
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
x <- 7*41/pi
x numeric
[1] 91.35494
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
[1] 2 4 5
LISTS IN R
# COPY}
x[[2]]
# we can modify its content directly
x[[2]][1]="ta"
x[[2]]
s # is not affected
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
# 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
# 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
DATAFRAME IN R
✓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
# 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