Vector and Matrix in R
Creating vector in R
x = c(2,7,5)
x
[1] 2 7 5
y = seq(from = 4, length = 3, by = 3)
y
[1] 4 7 10
Vector Operation
x + y
[1] 6 14 15
x^y
[1] 16 823543 9765625
x[2]
[1] 7
x[2:3]
[1] 7 5
x[-2] ## remove the 2nd elements
[1] 2 5
Create matrix in R
z = matrix(seq(1,12),4,3)
z
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12
z[3:4,2:3]
[,1] [,2]
[1,] 7 11
[2,] 8 12
z[,2:3]
[,1] [,2]
[1,] 5 9
[2,] 6 10
[3,] 7 11
[4,] 8 12
z[,1] ## will return as vector type
[1] 1 2 3 4
z[,1,drop = FALSE] ## Remain Matrix
[,1]
[1,] 1
[2,] 2
[3,] 3
[4,] 4
dim(z) ## Check dimension
[1] 4 3
Other Useful function
ls() ## list all variables within the env
[1] "x" "y" "z"
rm(y) ## Remove a typical variable
ls()
[1] "x" "z"
Data Simulating
Generate Random data, graphics
set.seed(11)
x = runif(50) ## 50 random variable follows uniform distribution
y = rnorm(50) ## 50 random variable follows standard normal distribution
plot(x,y, xlab = "Random Uniform", ylab = "Random Noraml", pch = "*", col = "blue")
Make parallel plot
par(mfrow = c(2,1)) ## 2 rows, 1 cols
plot(x,y, xlab = "Random Uniform", ylab = "Random Noraml", pch = "*", col = "blue")
hist(y)
par(mfrow = c(1,1)) ## Restore the setting
Example
auto = read.csv("./Data/Auto.csv") ### Read data
names(auto) ## check all variables
[1] "mpg" "cylinders" "displacement" "horsepower" "weight" "acceleration" "year"
[8] "origin" "name"
summary(auto)
mpg cylinders displacement horsepower weight acceleration year
Min. : 9.00 Min. :3.000 Min. : 68.0 Length:397 Min. :1613 Min. : 8.00 Min. :70.00
1st Qu.:17.50 1st Qu.:4.000 1st Qu.:104.0 Class :character 1st Qu.:2223 1st Qu.:13.80 1st Qu.:73.00
Median :23.00 Median :4.000 Median :146.0 Mode :character Median :2800 Median :15.50 Median :76.00
Mean :23.52 Mean :5.458 Mean :193.5 Mean :2970 Mean :15.56 Mean :75.99
3rd Qu.:29.00 3rd Qu.:8.000 3rd Qu.:262.0 3rd Qu.:3609 3rd Qu.:17.10 3rd Qu.:79.00
Max. :46.60 Max. :8.000 Max. :455.0 Max. :5140 Max. :24.80 Max. :82.00
origin name
Min. :1.000 Length:397
1st Qu.:1.000 Class :character
Median :1.000 Mode :character
Mean :1.574
3rd Qu.:2.000
Max. :3.000
table(auto$horsepower)
? 100 102 103 105 107 108 110 112 113 115 116 120 122 125 129 130 132 133 135 137 138 139 140 142 145 148 149
5 17 1 1 12 1 1 18 3 1 5 1 4 1 3 2 5 1 1 1 1 1 2 7 1 7 1 1
150 152 153 155 158 160 165 167 170 175 180 190 193 198 200 208 210 215 220 225 230 46 48 49 52 53 54 58
22 1 2 2 1 2 4 1 5 5 5 3 1 2 1 1 1 3 1 3 1 2 3 1 4 2 1 2
60 61 62 63 64 65 66 67 68 69 70 71 72 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
5 1 2 3 1 10 1 12 6 3 12 5 6 3 14 4 1 6 2 7 2 1 4 6 9 5 2 19
89 90 91 92 93 94 95 96 97 98
1 20 1 6 1 1 14 3 9 2
dim(auto)
[1] 397 9
class(auto)
[1] "data.frame"
plot(auto$cylinders, auto$mpg)
lowess_model <- lowess(auto$cylinders, auto$mpg)
lines(lowess_model, col="red") ### add the lowess soomth line
attach(auto) ## make a columns as an variable and store into the env
search()
[1] ".GlobalEnv" "auto" "tools:rstudio" "package:stats" "package:graphics"
[6] "package:grDevices" "package:utils" "package:datasets" "package:methods" "Autoloads"
[11] "package:base"
## boxplot
plot(as.factor(cylinders),mpg)
LS0tDQp0aXRsZTogIkNoYXB0ZXIyIFN0YXRpc3RpY2FsIExlYXJuaW5nIg0KYXV0aG9yOiAiWWFsaW4gWWFuZyINCmRhdGU6ICJgciBTeXMuRGF0ZSgpYCINCm91dHB1dDoNCiAgaHRtbF9ub3RlYm9vazoNCiAgICB0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