Sally Chen
8/25/2018
## [1] "numeric"
## [1] "character"
## [1] "logical"
## [1] 100
## [1] 0.02
## Error: <text>:1:2: unexpected symbol
## 1: 1x
## ^
## [1] 200
## Error in 2 + "2": non-numeric argument to binary operator
## num [1:4] 1 2 3 4
## chr [1:4] "1" "2" "hello" "R"
## [1] 0 0 0 0 0
## [1] "" "" ""
## [1] "1" "hello" "TRUE"
## [1] 3 4 5 6 7
## [1] 3 3 3 3
## [1] 1 2 3 4
## [1] 4
## [1] 1.290994
## [1] 1
## [1] 2 3 4 5
## [1] 2.718282 7.389056 20.085537 54.598150
## [1] 11 2 3 4
## [1] 11 2 3 4
## [1] 22 6 12 20
## [1] 60
## [1] 11 2 3 4
## [1] 11
## [1] 11 2 3
## [1] 11 3
## [1] 11 2 4
## [1] "character"
## Warning in mean.default(z): argument is not numeric or logical: returning
## NA
## [1] NA
## [1] FALSE
## [1] 1 2 3
## [1] 2
## [1] NA
## [1] FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE
## [1] 12.375
use function() to create a new function
Structure of a function
## [1] 1.1 1.4 1.7 2.0 2.3 2.6 2.9 3.2 3.5
## [1] 1.1 1.4 1.7 2.0 2.3 2.6 2.9 3.2 3.5
## [1] 1.1 1.4 1.7 2.0 2.3 2.6 2.9 3.2 3.5
## [1] 1.1 1.4 1.7 2.0 2.3 2.6 2.9 3.2 3.5
## [1] 0.48066306 -0.73348538 -1.23786307 -1.43250440 1.16290540
## [6] 1.01817997 -0.01531768 -1.97142729 0.80005467 -0.80021797
## [1] 99.07570 100.44479 100.47408 99.79697 101.49242 100.54867 98.97238
## [8] 98.09214 99.62634 100.55920
simplesum = function(a, b) { #attributes
s = a+b; #statement
return(s); }
print(simplesum(a=10,b=20))## [1] 30
## [1] -10
## [1] 10
## [,1] [,2]
## [1,] 1 4
## [2,] 2 5
## [3,] 3 6
## [,1] [,2]
## [1,] 1 2
## [2,] 3 4
## [3,] 5 6
## [,1]
## [1,] 1
## [2,] 2
## [3,] 3
## [4,] 4
## [5,] 5
## [6,] 6
## a1 a2
## [1,] 1 4
## [2,] 2 5
## [3,] 3 6
## Warning in cbind(a1, a2): number of rows of result is not a multiple of
## vector length (arg 2)
## a1 a2
## [1,] 1 4
## [2,] 2 5
## [3,] 3 4
## Warning in rbind(a1, a2): number of columns of result is not a multiple of
## vector length (arg 2)
## [,1] [,2] [,3]
## a1 1 2 3
## a2 4 5 4
## [,1]
## [1,] "1"
## [2,] "2"
## [3,] "3"
## [4,] "4"
## [5,] "5"
## [6,] "hello"
## [,1] [,2]
## [1,] 1 4
## [2,] 2 5
## [3,] 3 6
## [1] 1
## [1] 1 4
## [1] 4 5 6
## [,1] [,2]
## [1,] 1 4
## [2,] 2 5
## [,1] [,2]
## [1,] 1 4
## [2,] 2 5
## [3,] 3 6
## [,1] [,2]
## [1,] 2 5
## [2,] 3 6
## [3,] 4 7
## [,1] [,2]
## [1,] 1 16
## [2,] 4 25
## [3,] 9 36
## [1] 21
## [1] 5 7 9
## [1] 6 15
name = c("Messi","Ronaldo","Neymar")
age = c(30,32,25)
golden_ball = c(TRUE,TRUE,FALSE)
players = data.frame(name,age,golden_ball)
head(players)## name age golden_ball
## 1 Messi 30 TRUE
## 2 Ronaldo 32 TRUE
## 3 Neymar 25 FALSE
golden_ball = c(TRUE,TRUE) # golden ball has one missing value
data.frame(name,age,golden_ball) # data frame do not accept columns of different lengths## Error in data.frame(name, age, golden_ball): arguments imply differing number of rows: 3, 2
## name age golden_ball
## 1 Messi 30 TRUE
## 2 Ronaldo 32 TRUE
## 3 Neymar 25 NA
## [,1] [,2] [,3]
## [1,] 1 3 5
## [2,] 2 4 6
## V1 V2 V3
## 1 1 3 5
## 2 2 4 6
## name age golden_ball
## 1 Messi 30 TRUE
## [1] Messi Ronaldo Neymar
## Levels: Messi Neymar Ronaldo
## [1] Messi Ronaldo Neymar
## Levels: Messi Neymar Ronaldo
## [1] Ronaldo
## Levels: Messi Neymar Ronaldo
## [1] 32 34 27
## name age golden_ball goals
## 1 Messi 30 TRUE 20
## 2 Ronaldo 32 TRUE 30
## 3 Neymar 25 FALSE 10
new_player = data.frame(name = "Suarez", age = 30, golden_ball= FALSE, goals = 40) # add a new row to existing data.frame
rbind(players,new_player)## name age golden_ball goals
## 1 Messi 30 TRUE 20
## 2 Ronaldo 32 TRUE 30
## 3 Neymar 25 FALSE 10
## 4 Suarez 30 FALSE 40
R uses list for complex, hierarchical objects
Use list() to construct
## [[1]]
## name age golden_ball goals
## 1 Messi 30 TRUE 20
## 2 Ronaldo 32 TRUE 30
## 3 Neymar 25 FALSE 10
## 4 Suarez 30 FALSE 40
##
## [[2]]
## [1] 1 2 3
##
## [[3]]
## [,1]
## [1,] 1
## [2,] 2
## [3,] 3
## [4,] 4
## [5,] 5
## [6,] 6
## name age golden_ball goals
## 1 Messi 30 TRUE 20
## 2 Ronaldo 32 TRUE 30
## 3 Neymar 25 FALSE 10
## 4 Suarez 30 FALSE 40
## [1] "North" "West" "North" "East" "South" "West"
## [1] "character"
## Length Class Mode
## 6 character character
## [1] North West North East South West
## Levels: East North South West
## [1] "factor"
## East North South West
## 1 2 1 2