Professional Documents
Culture Documents
Rinstallation
Rinstallation
2023-04-13
x <- 1
print(x)
## [1] 1
## [1] 1
Evaluation
x <- 5
x
## [1] 5
print(x)
## [1] 5
x <- 10:30
x
## [1] 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
Creating Vectors
1
x <- c(0.5, 0.6)
x <- c(T, F)
x <- 9:29
## [1] 0 0 0 0 0 0 0 0 0 0
R-Graph Plotting
library(datasets)
data(mtcars)
head(mtcars)
summary(mtcars)
2
## Max. :4.930 Max. :5.424 Max. :22.90 Max. :1.0000
## am gear carb
## Min. :0.0000 Min. :3.000 Min. :1.000
## 1st Qu.:0.0000 1st Qu.:3.000 1st Qu.:2.000
## Median :0.0000 Median :4.000 Median :2.000
## Mean :0.4062 Mean :3.688 Mean :2.812
## 3rd Qu.:1.0000 3rd Qu.:4.000 3rd Qu.:4.000
## Max. :1.0000 Max. :5.000 Max. :8.000
boxplot(mtcars$mpg, col="blue")
30
25
20
15
10
3
Histogram of mtcars$mpg
12
10
8
Frequency
6
4
2
0
10 15 20 25 30 35
mtcars$mpg
4
Histogram of mtcars$mpg
4
3
Frequency
2
1
0
10 15 20 25 30
mtcars$mpg
5
Histogram of mtcars$mpg
3
2
Frequency
1
0
10 15 20 25 30
mtcars$mpg
barplot(table(mtcars$carb), col="yellow")
6
10
8
6
4
2
0
1 2 3 4 6 8
7
30
25
mpg
20
15
10
3 4 5
gear
8
Histogram of subset(mtcars, cyl == 4)$mpg
2
Frequency
1
0
20 22 24 26 28 30 32 34
9
Histogram of subset(mtcars, cyl == 8)$mpg
7
6
5
Frequency
4
3
2
1
0
10 12 14 16 18 20
10
22
20
qsec
18
16
10 15 20 25 30
mpg
Mixing Objects
Explicit Coercion
x <- 0:6
class(x)
## [1] "integer"
as.numeric(x)
## [1] 0 1 2 3 4 5 6
11
as.logical(x)
as.character(x)
as.numeric(x)
## [1] NA NA NA
as.logical(x)
## [1] NA NA NA
as.complex(x)
## [1] NA NA NA
Matrices
dim(m)
## [1] 2 3
attributes(m)
## $dim
## [1] 2 3
12
m <- matrix(1:6, nrow = 2, ncol = 3)
m
m <- 1:10
m
## [1] 1 2 3 4 5 6 7 8 9 10
x <- 1:3
y <- 10:12
cbind(x, y)
## x y
## [1,] 1 10
## [2,] 2 11
## [3,] 3 12
rbind(x, y)
Lists
## [[1]]
## [1] 1
##
## [[2]]
## [1] "a"
##
## [[3]]
## [1] TRUE
##
## [[4]]
## [1] 1+4i
13
x <- vector("list", length = 5)
x
## [[1]]
## NULL
##
## [[2]]
## NULL
##
## [[3]]
## NULL
##
## [[4]]
## NULL
##
## [[5]]
## NULL
Factors
table(x)
## x
## no yes
## 2 3
## [1] 2 2 1 2 1
## attr(,"levels")
## [1] "no" "yes"
14
## Return a logical vector indicating which elements are NaN
is.nan(x)
is.na(x)
is.nan(x)
Data Frames
## foo bar
## 1 1 TRUE
## 2 2 TRUE
## 3 3 FALSE
## 4 4 FALSE
nrow(x)
## [1] 4
ncol(x)
## [1] 2
Names
x <- 1:3
names(x)
## NULL
15
names(x) <- c("New York", "Seattle", "Los Angeles")
names(x)
## $‘Los Angeles‘
## [1] 1
##
## $Boston
## [1] 2
##
## $London
## [1] 3
names(x)
## c d
## a 1 3
## b 2 4
## h f
## x 1 3
## z 2 4
16