# calculator
2+2
## [1] 4
27/3
## [1] 9
3^9
## [1] 19683
3**9
## [1] 19683
3**9+1
## [1] 19684
3**(9+1)
## [1] 59049
sum(1, 2, 5,29, 43)
## [1] 80
prod(1, 2, 5,29, 43)
## [1] 12470
50 %% 3
## [1] 2
abs(-53)
## [1] 53
factorial(20)
## [1] 2.432902e+18
pi
## [1] 3.141593
exp(1)
## [1] 2.718282
choose(7, 3)
## [1] 35
27^(1/3)
## [1] 3
log(4)
## [1] 1.386294
log10(39)
## [1] 1.591065
34+2i
## [1] 34+2i
9^(1/2)
## [1] 3
27^(1/3)
## [1] 3
General units
typeof(3)
## [1] "double"
typeof(5L)
## [1] "integer"
typeof("Hi all")
## [1] "character"
typeof(43 + 4i)
## [1] "complex"
typeof(TRUE)
## [1] "logical"
typeof(F)
## [1] "logical"
vectors
1:40
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
## [24] 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
40:1*10
## [1] 400 390 380 370 360 350 340 330 320 310 300 290 280 270 260 250 240
## [18] 230 220 210 200 190 180 170 160 150 140 130 120 110 100 90 80 70
## [35] 60 50 40 30 20 10
c("a", "b", "ку-ку")
## [1] "a" "b" "ку-ку"
c("a", "b", "ку-ку", 2)
## [1] "a" "b" "ку-ку" "2"
c(T, F, T, T)
## [1] TRUE FALSE TRUE TRUE
c(T, F, T, T, 1)
## [1] 1 0 1 1 1
sum(c(T, F, T, T))
## [1] 3
sum(1:50 > 30)
## [1] 20
1:25*2-1
## [1] 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45
## [24] 47 49
seq(2, 10, 2)
## [1] 2 4 6 8 10
seq(from = 2, to = 20, by = 3)
## [1] 2 5 8 11 14 17 20
seq(by = 3, to = 20, from = 2)
## [1] 2 5 8 11 14 17 20
seq(b = 3, t = 20, f = 2)
## [1] 2 5 8 11 14 17 20
seq(2, 20, 3)
## [1] 2 5 8 11 14 17 20
letters
## [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q"
## [18] "r" "s" "t" "u" "v" "w" "x" "y" "z"
LETTERS
## [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q"
## [18] "R" "S" "T" "U" "V" "W" "X" "Y" "Z"
month.abb
## [1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov"
## [12] "Dec"
month.name
## [1] "January" "February" "March" "April" "May"
## [6] "June" "July" "August" "September" "October"
## [11] "November" "December"
sample(1:6, size = 2)
## [1] 3 6
sample(1:6, size = 2, replace = T)
## [1] 4 2
set.seed(19012017)
sample(1:6, size = 2, replace = T)
## [1] 4 5
sample(1:6, size = 2, replace = T)
## [1] 3 4
runif(10, min = 20, max = 50)
## [1] 34.02226 26.80585 34.55968 37.68316 42.71555 24.91827 30.37555
## [8] 30.64998 22.49337 30.88836
Variables
a <- 1:19
a
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
a+1
## [1] 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
a
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
a <- a + 1
a
## [1] 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
b <- a
b <- b + 100
a
## [1] 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
a = 1:19 # not cool
length(a)
## [1] 19
length("1234567890")
## [1] 1
nchar("1234567890")
## [1] 10
a <- c(Masha = 5, Vanya = 7, Anya = 4, Masha = 3)
a
## Masha Vanya Anya Masha
## 5 7 4 3
a <- c('Masha' = 1:5, 'Vanya' = 7, 'Anya' = 4, 'Masha' = 3)
a
## Masha1 Masha2 Masha3 Masha4 Masha5 Vanya Anya Masha
## 1 2 3 4 5 7 4 3
a["Vanya"]
## Vanya
## 7
a["Masha"]
## Masha
## 3
Matrix
matrix(1:10)
## [,1]
## [1,] 1
## [2,] 2
## [3,] 3
## [4,] 4
## [5,] 5
## [6,] 6
## [7,] 7
## [8,] 8
## [9,] 9
## [10,] 10
matrix(1:10, nrow = 5)
## [,1] [,2]
## [1,] 1 6
## [2,] 2 7
## [3,] 3 8
## [4,] 4 9
## [5,] 5 10
matrix(1:10, ncol = 5)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 3 5 7 9
## [2,] 2 4 6 8 10
m <- matrix(1:10)
dim(m)
## [1] 10 1
dim(m) <- c(2, 5)
m
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 3 5 7 9
## [2,] 2 4 6 8 10
Lists
a <- list(a = letters,
b = 1:20,
c = c(T, T, F, F, F))
a
## $a
## [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q"
## [18] "r" "s" "t" "u" "v" "w" "x" "y" "z"
##
## $b
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
##
## $c
## [1] TRUE TRUE FALSE FALSE FALSE
Dataframe
df <- data.frame(length = c(1.78, 74, 89, 23),
weight = c(40, 50, 20, 90),
names = c("Dyumovochka", "Masha", "Tanya", "Stepa"),
get.up.early = c(T, T, F, F))
df
## length weight names get.up.early
## 1 1.78 40 Dyumovochka TRUE
## 2 74.00 50 Masha TRUE
## 3 89.00 20 Tanya FALSE
## 4 23.00 90 Stepa FALSE
mtcars
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
## Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
## Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
## Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
## Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
## Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
## Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3
## Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3
## Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3
## Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4
## Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4
## Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4
## Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
## Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
## Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
## Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
## Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2
## AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2
## Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4
## Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2
## Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
## Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
## Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
## Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4
## Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6
## Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8
## Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
#? cars
apropos("date")
## [1] "as.character.Date" "as.data.frame.Date" "as.Date"
## [4] "as.Date.character" "as.Date.date" "as.Date.dates"
## [7] "as.Date.default" "as.Date.factor" "as.Date.numeric"
## [10] "as.Date.POSIXct" "as.Date.POSIXlt" "as.list.Date"
## [13] "as.POSIXct.date" "as.POSIXct.Date" "as.POSIXct.dates"
## [16] "as.POSIXlt.date" "as.POSIXlt.Date" "as.POSIXlt.dates"
## [19] "axis.Date" "c.Date" "cut.Date"
## [22] "date" "-.Date" "[<-.Date"
## [25] "[.Date" "[[.Date" "+.Date"
## [28] "diff.Date" "format.Date" "is.numeric.Date"
## [31] "ISOdate" "ISOdatetime" "julian.Date"
## [34] "Math.Date" "mean.Date" "months.Date"
## [37] "Ops.Date" "print.Date" "quarters.Date"
## [40] "rep.Date" "round.Date" "seq.Date"
## [43] "split.Date" "summary.Date" "Summary.Date"
## [46] "Sys.Date" "trunc.Date" "update"
## [49] "update.default" "update.formula" "update.packages"
## [52] "update.packageStatus" "weekdays.Date" "xtfrm.Date"
Logic
"mama" == "mama"
## [1] TRUE
"mama" == "tama"
## [1] FALSE
"mama" != "tama"
## [1] TRUE
"mama" > "tama"
## [1] FALSE
"mama" < "tama"
## [1] TRUE
"mama" < c("tama", "bama")
## [1] TRUE FALSE
3 <= 5
## [1] TRUE
10 >= 100
## [1] FALSE
10 > -Inf
## [1] TRUE
10 < Inf
## [1] TRUE
2+2 == 4
## [1] TRUE
10/3 == 100/3-30
## [1] FALSE
TRUE & FALSE
## [1] FALSE
TRUE | FALSE
## [1] TRUE
TRUE | c(FALSE, TRUE)
## [1] TRUE TRUE
TRUE & c(FALSE, TRUE)
## [1] FALSE TRUE
0.2+0.2 == 0.4
## [1] TRUE
0.1+0.2 == 0.3 # Floating point representation of a number is always an approximation!
## [1] FALSE
read more about it
Indexing
a <- 1:100
a[45]
## [1] 45
1:100 -> q
q
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
## [18] 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
## [35] 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
## [52] 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
## [69] 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
## [86] 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
a <- c(masha = 1:100)
a["masha43"]
## masha43
## 43
a[a > 40]
## masha41 masha42 masha43 masha44 masha45 masha46 masha47 masha48
## 41 42 43 44 45 46 47 48
## masha49 masha50 masha51 masha52 masha53 masha54 masha55 masha56
## 49 50 51 52 53 54 55 56
## masha57 masha58 masha59 masha60 masha61 masha62 masha63 masha64
## 57 58 59 60 61 62 63 64
## masha65 masha66 masha67 masha68 masha69 masha70 masha71 masha72
## 65 66 67 68 69 70 71 72
## masha73 masha74 masha75 masha76 masha77 masha78 masha79 masha80
## 73 74 75 76 77 78 79 80
## masha81 masha82 masha83 masha84 masha85 masha86 masha87 masha88
## 81 82 83 84 85 86 87 88
## masha89 masha90 masha91 masha92 masha93 masha94 masha95 masha96
## 89 90 91 92 93 94 95 96
## masha97 masha98 masha99 masha100
## 97 98 99 100
mtcars$cyl[17]
## [1] 8
mtcars[3, 7] # first rows, second columns
## [1] 18.61
mtcars[mtcars$cyl !=6, ]
## mpg cyl disp hp drat wt qsec vs am gear carb
## Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
## Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
## Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
## Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
## Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
## Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3
## Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3
## Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3
## Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4
## Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4
## Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4
## Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
## Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
## Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
## Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
## Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2
## AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2
## Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4
## Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2
## Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
## Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
## Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
## Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4
## Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8
## Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
mtcars[mtcars$cyl !=6, 1:3]
## mpg cyl disp
## Datsun 710 22.8 4 108.0
## Hornet Sportabout 18.7 8 360.0
## Duster 360 14.3 8 360.0
## Merc 240D 24.4 4 146.7
## Merc 230 22.8 4 140.8
## Merc 450SE 16.4 8 275.8
## Merc 450SL 17.3 8 275.8
## Merc 450SLC 15.2 8 275.8
## Cadillac Fleetwood 10.4 8 472.0
## Lincoln Continental 10.4 8 460.0
## Chrysler Imperial 14.7 8 440.0
## Fiat 128 32.4 4 78.7
## Honda Civic 30.4 4 75.7
## Toyota Corolla 33.9 4 71.1
## Toyota Corona 21.5 4 120.1
## Dodge Challenger 15.5 8 318.0
## AMC Javelin 15.2 8 304.0
## Camaro Z28 13.3 8 350.0
## Pontiac Firebird 19.2 8 400.0
## Fiat X1-9 27.3 4 79.0
## Porsche 914-2 26.0 4 120.3
## Lotus Europa 30.4 4 95.1
## Ford Pantera L 15.8 8 351.0
## Maserati Bora 15.0 8 301.0
## Volvo 142E 21.4 4 121.0
rownames(mtcars[mtcars$cyl !=6, ])[1:3]
## [1] "Datsun 710" "Hornet Sportabout" "Duster 360"
# View(mtcars)
d <- mtcars
Library
#install.packages("lingtypology")
library(lingtypology)
map.feature(c("Russian", "Abaza", "Adyghe"))
Loops
a <- 2
if(a > 3){
print("hello")
} else {
print("bye")
}
## [1] "bye"
for(i in 1:100){
print(i %% 2)
}
## [1] 1
## [1] 0
## [1] 1
## [1] 0
## [1] 1
## [1] 0
## [1] 1
## [1] 0
## [1] 1
## [1] 0
## [1] 1
## [1] 0
## [1] 1
## [1] 0
## [1] 1
## [1] 0
## [1] 1
## [1] 0
## [1] 1
## [1] 0
## [1] 1
## [1] 0
## [1] 1
## [1] 0
## [1] 1
## [1] 0
## [1] 1
## [1] 0
## [1] 1
## [1] 0
## [1] 1
## [1] 0
## [1] 1
## [1] 0
## [1] 1
## [1] 0
## [1] 1
## [1] 0
## [1] 1
## [1] 0
## [1] 1
## [1] 0
## [1] 1
## [1] 0
## [1] 1
## [1] 0
## [1] 1
## [1] 0
## [1] 1
## [1] 0
## [1] 1
## [1] 0
## [1] 1
## [1] 0
## [1] 1
## [1] 0
## [1] 1
## [1] 0
## [1] 1
## [1] 0
## [1] 1
## [1] 0
## [1] 1
## [1] 0
## [1] 1
## [1] 0
## [1] 1
## [1] 0
## [1] 1
## [1] 0
## [1] 1
## [1] 0
## [1] 1
## [1] 0
## [1] 1
## [1] 0
## [1] 1
## [1] 0
## [1] 1
## [1] 0
## [1] 1
## [1] 0
## [1] 1
## [1] 0
## [1] 1
## [1] 0
## [1] 1
## [1] 0
## [1] 1
## [1] 0
## [1] 1
## [1] 0
## [1] 1
## [1] 0
## [1] 1
## [1] 0
## [1] 1
## [1] 0
## [1] 1
## [1] 0
x <- c()
for(i in 1:100){
x[i] <- i %% 2
}
x
## [1] 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
## [36] 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
## [71] 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
i <- 0
while(i < 10){
print(i)
i <- i+1
}
## [1] 0
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5
## [1] 6
## [1] 7
## [1] 8
## [1] 9
Functions
micatron <- function(zh, b, c){
sum(zh, b)/c
}
micatron(11, 10, 10)
## [1] 2.1
micatron(b = 10, 10, zh = 11)
## [1] 2.1
my_sum <- function(x, y = 10){
x+y
}
my_sum(11)
## [1] 21
my_sum(11, 12)
## [1] 23
Apply, sapply
mtcars
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
## Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
## Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
## Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
## Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
## Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
## Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3
## Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3
## Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3
## Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4
## Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4
## Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4
## Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
## Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
## Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
## Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
## Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2
## AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2
## Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4
## Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2
## Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
## Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
## Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
## Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4
## Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6
## Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8
## Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
str(mtcars)
## 'data.frame': 32 obs. of 11 variables:
## $ mpg : num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
## $ cyl : num 6 6 4 6 8 6 8 4 4 6 ...
## $ disp: num 160 160 108 258 360 ...
## $ hp : num 110 110 93 110 175 105 245 62 95 123 ...
## $ drat: num 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
## $ wt : num 2.62 2.88 2.32 3.21 3.44 ...
## $ qsec: num 16.5 17 18.6 19.4 17 ...
## $ vs : num 0 0 1 1 0 1 0 1 1 1 ...
## $ am : num 1 1 1 0 0 0 0 0 0 0 ...
## $ gear: num 4 4 4 3 3 3 3 4 4 4 ...
## $ carb: num 4 4 1 1 2 1 4 2 2 4 ...
summary(mtcars)
## mpg cyl disp hp
## Min. :10.40 Min. :4.000 Min. : 71.1 Min. : 52.0
## 1st Qu.:15.43 1st Qu.:4.000 1st Qu.:120.8 1st Qu.: 96.5
## Median :19.20 Median :6.000 Median :196.3 Median :123.0
## Mean :20.09 Mean :6.188 Mean :230.7 Mean :146.7
## 3rd Qu.:22.80 3rd Qu.:8.000 3rd Qu.:326.0 3rd Qu.:180.0
## Max. :33.90 Max. :8.000 Max. :472.0 Max. :335.0
## drat wt qsec vs
## Min. :2.760 Min. :1.513 Min. :14.50 Min. :0.0000
## 1st Qu.:3.080 1st Qu.:2.581 1st Qu.:16.89 1st Qu.:0.0000
## Median :3.695 Median :3.325 Median :17.71 Median :0.0000
## Mean :3.597 Mean :3.217 Mean :17.85 Mean :0.4375
## 3rd Qu.:3.920 3rd Qu.:3.610 3rd Qu.:18.90 3rd Qu.:1.0000
## 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
apply(mtcars, 1, mean)
## Mazda RX4 Mazda RX4 Wag Datsun 710
## 29.90727 29.98136 23.59818
## Hornet 4 Drive Hornet Sportabout Valiant
## 38.73955 53.66455 35.04909
## Duster 360 Merc 240D Merc 230
## 59.72000 24.63455 27.23364
## Merc 280 Merc 280C Merc 450SE
## 31.86000 31.78727 46.43091
## Merc 450SL Merc 450SLC Cadillac Fleetwood
## 46.50000 46.35000 66.23273
## Lincoln Continental Chrysler Imperial Fiat 128
## 66.05855 65.97227 19.44091
## Honda Civic Toyota Corolla Toyota Corona
## 17.74227 18.81409 24.88864
## Dodge Challenger AMC Javelin Camaro Z28
## 47.24091 46.00773 58.75273
## Pontiac Firebird Fiat X1-9 Porsche 914-2
## 57.37955 18.92864 24.77909
## Lotus Europa Ford Pantera L Ferrari Dino
## 24.88027 60.97182 34.50818
## Maserati Bora Volvo 142E
## 63.15545 26.26273
apply(mtcars, 2, mean)
## mpg cyl disp hp drat wt
## 20.090625 6.187500 230.721875 146.687500 3.596563 3.217250
## qsec vs am gear carb
## 17.848750 0.437500 0.406250 3.687500 2.812500
apply(mtcars, 2, function(x){mean(x)+ min(x)})
## mpg cyl disp hp drat wt
## 30.490625 10.187500 301.821875 198.687500 6.356562 4.730250
## qsec vs am gear carb
## 32.348750 0.437500 0.406250 6.687500 3.812500
nchar(month.name)^2
## [1] 49 64 25 25 9 16 16 36 81 49 64 64
sapply(month.name, function(x){nchar(x)^2})
## January February March April May June July
## 49 64 25 25 9 16 16
## August September October November December
## 36 81 49 64 64