Жарикова Анастасия Александровна - azharikova89@gmail.com
Пензар Дмитрий Дмитриевич - dmitrypenzar1996@gmail.com
Валяева Анна Александровна - valyaeva.ann@gmail.com
Ходите на занятия
Чат ТГ "ФББ набор 2019, Алгоритмы и R"
Комната 633, корпус Б
В RStudio: File -> New File -> R Markdown -> пример документа
Запускается целиком, сразу видны ошибки и проблемы
Фрагменты кода записывают в “chunk”:
Переменные и функции, инициализированные в одном chunk, будут работать во всем документе
В случае ошибки в коде итоговый документ не будет создан, появится сообщение об ошибке
Читаем ошибку - исправляем - перезапускаем
https://rmarkdown.rstudio.com/index.html
https://www.rstudio.com/wp-content/uploads/2015/03/rmarkdown-reference.pdf
Должно быть ясно:
getwd() # узнать рабочую директорию
setwd(‘Newdir’) # задать рабочую директорию
dir() # список файлов в рабочей директории
ls() # список переменных в рабочей дирректории
sessionInfo() # информация о загруженной сессии R
x <- 1:5x
[1] 1 2 3 4 5
x <- 1:5xy <- 6:10y
[1] 1 2 3 4 5
[1] 6 7 8 9 10
x <- 1:5xy <- 6:10ylength(x)
[1] 1 2 3 4 5
[1] 6 7 8 9 10
[1] 5
Что такое [1]?
vec <- 100:120 vec
[1] 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118[20] 119 120
x <- 1:5x
[1] 1 2 3 4 5
x <- 1:5xy <- 6:10y
[1] 1 2 3 4 5
[1] 6 7 8 9 10
x <- 1:5xy <- 6:10yx + y
[1] 1 2 3 4 5
[1] 6 7 8 9 10
[1] 7 9 11 13 15
x <- 1:5xy <- 6:10yx + yx^2
[1] 1 2 3 4 5
[1] 6 7 8 9 10
[1] 7 9 11 13 15
[1] 1 4 9 16 25
x <- 1:5xy <- 6:10yx + yx^2x+3
[1] 1 2 3 4 5
[1] 6 7 8 9 10
[1] 7 9 11 13 15
[1] 1 4 9 16 25
[1] 4 5 6 7 8
x <- 1:5xy <- 6:10yx + yx^2x+3-x
[1] 1 2 3 4 5
[1] 6 7 8 9 10
[1] 7 9 11 13 15
[1] 1 4 9 16 25
[1] 4 5 6 7 8
[1] -1 -2 -3 -4 -5
x <- 1:5xy <- 6:10yx + yx^2x+3-xx > 4
[1] 1 2 3 4 5
[1] 6 7 8 9 10
[1] 7 9 11 13 15
[1] 1 4 9 16 25
[1] 4 5 6 7 8
[1] -1 -2 -3 -4 -5
[1] FALSE FALSE FALSE FALSE TRUE
x <- 1:5xy <- 6:10yx + yx^2x+3-xx > 4x == 4
[1] 1 2 3 4 5
[1] 6 7 8 9 10
[1] 7 9 11 13 15
[1] 1 4 9 16 25
[1] 4 5 6 7 8
[1] -1 -2 -3 -4 -5
[1] FALSE FALSE FALSE FALSE TRUE
[1] FALSE FALSE FALSE TRUE FALSE
x <- 1:5xy <- 6:10yx + yx^2x+3-xx > 4x == 4x = 4
[1] 1 2 3 4 5
[1] 6 7 8 9 10
[1] 7 9 11 13 15
[1] 1 4 9 16 25
[1] 4 5 6 7 8
[1] -1 -2 -3 -4 -5
[1] FALSE FALSE FALSE FALSE TRUE
[1] FALSE FALSE FALSE TRUE FALSE
x <- 1:5xy <- 6:10yx + yx^2x+3-xx > 4x == 4x = 4x
[1] 1 2 3 4 5
[1] 6 7 8 9 10
[1] 7 9 11 13 15
[1] 1 4 9 16 25
[1] 4 5 6 7 8
[1] -1 -2 -3 -4 -5
[1] FALSE FALSE FALSE FALSE TRUE
[1] FALSE FALSE FALSE TRUE FALSE
[1] 4
x <- 1:5 x
[1] 1 2 3 4 5
y <- 1:2 y
[1] 1 2
x+y
Warning in x + y: longer object length is not a multiple of shorter objectlength
[1] 2 4 4 6 6
c(1,2,3)
[1] 1 2 3
x <- 1:3x
[1] 1 2 3
x <- c(x,5:7)x
[1] 1 2 3 5 6 7
1:10
[1] 1 2 3 4 5 6 7 8 9 10
seq(from=1,to=8,by=2)
[1] 1 3 5 7
rep(1:3,times=3)
[1] 1 2 3 1 2 3 1 2 3
sample(1:30,10,replace=T)
[1] 13 12 7 23 30 29 24 16 13 5
set.seed(123)rpois(20,10)
[1] 8 9 14 10 10 15 11 5 4 13 11 11 10 8 15 11 3 7 6 8
set.seed(123)a <- rpois(20,10)sum(a)
[1] 189
sd(a)
[1] 3.410124
max(a)
[1] 15
mean(a)
[1] 9.45
x <- c(T,F,F,T)typeof(x)
[1] "logical"
x <- c(T,F,F,T)typeof(x)x <- c(0.5, 1.2, 3.6)typeof(x)
[1] "logical"
[1] "double"
x <- c(T,F,F,T)typeof(x)x <- c(0.5, 1.2, 3.6)typeof(x)x <- 1:5typeof(x)
[1] "logical"
[1] "double"
[1] "integer"
x <- c(T,F,F,T)typeof(x)x <- c(0.5, 1.2, 3.6)typeof(x)x <- 1:5typeof(x)x <- c('a','b','c')typeof(x)
[1] "logical"
[1] "double"
[1] "integer"
[1] "character"
x <- c(T,F,F,T)typeof(x)x <- c(0.5, 1.2, 3.6)typeof(x)x <- 1:5typeof(x)x <- c('a','b','c')typeof(x)x <- c(F,1,2,T)typeof(x)
[1] "logical"
[1] "double"
[1] "integer"
[1] "character"
[1] "double"
x <- c(T,F,F,T)typeof(x)x <- c(0.5, 1.2, 3.6)typeof(x)x <- 1:5typeof(x)x <- c('a','b','c')typeof(x)x <- c(F,1,2,T)typeof(x)x
[1] "logical"
[1] "double"
[1] "integer"
[1] "character"
[1] "double"
[1] 0 1 2 1
x <- c(T,F,F,T)typeof(x)x <- c(0.5, 1.2, 3.6)typeof(x)x <- 1:5typeof(x)x <- c('a','b','c')typeof(x)x <- c(F,1,2,T)typeof(x)xx <- c(1,2.8)typeof(x)
[1] "logical"
[1] "double"
[1] "integer"
[1] "character"
[1] "double"
[1] 0 1 2 1
[1] "double"
x <- c(T,F,F,T)typeof(x)x <- c(0.5, 1.2, 3.6)typeof(x)x <- 1:5typeof(x)x <- c('a','b','c')typeof(x)x <- c(F,1,2,T)typeof(x)xx <- c(1,2.8)typeof(x)x
[1] "logical"
[1] "double"
[1] "integer"
[1] "character"
[1] "double"
[1] 0 1 2 1
[1] "double"
[1] 1.0 2.8
x <- c(T,F,F,T)typeof(x)x <- c(0.5, 1.2, 3.6)typeof(x)x <- 1:5typeof(x)x <- c('a','b','c')typeof(x)x <- c(F,1,2,T)typeof(x)xx <- c(1,2.8)typeof(x)xx <- c(1,'a',F,5.5)typeof(x)
[1] "logical"
[1] "double"
[1] "integer"
[1] "character"
[1] "double"
[1] 0 1 2 1
[1] "double"
[1] 1.0 2.8
[1] "character"
x <- c(T,F,F,T)typeof(x)x <- c(0.5, 1.2, 3.6)typeof(x)x <- 1:5typeof(x)x <- c('a','b','c')typeof(x)x <- c(F,1,2,T)typeof(x)xx <- c(1,2.8)typeof(x)xx <- c(1,'a',F,5.5)typeof(x)x
[1] "logical"
[1] "double"
[1] "integer"
[1] "character"
[1] "double"
[1] 0 1 2 1
[1] "double"
[1] 1.0 2.8
[1] "character"
[1] "1" "a" "FALSE" "5.5"
x <- c(5,16,8,32,56,2)x
[1] 5 16 8 32 56 2
x <- c(5,16,8,32,56,2)xx[1]
[1] 5 16 8 32 56 2
[1] 5
x <- c(5,16,8,32,56,2)xx[1]x[2:4]
[1] 5 16 8 32 56 2
[1] 5
[1] 16 8 32
x <- c(5,16,8,32,56,2)xx[1]x[2:4]x[c(2,5)]
[1] 5 16 8 32 56 2
[1] 5
[1] 16 8 32
[1] 16 56
x <- c(5,16,8,32,56,2)xx[1]x[2:4]x[c(2,5)]x[-1]
[1] 5 16 8 32 56 2
[1] 5
[1] 16 8 32
[1] 16 56
[1] 16 8 32 56 2
x <- c(5,16,8,32,56,2)xx[1]x[2:4]x[c(2,5)]x[-1]x > 10
[1] 5 16 8 32 56 2
[1] 5
[1] 16 8 32
[1] 16 56
[1] 16 8 32 56 2
[1] FALSE TRUE FALSE TRUE TRUE FALSE
x <- c(5,16,8,32,56,2)xx[1]x[2:4]x[c(2,5)]x[-1]x > 10x[x > 10]
[1] 5 16 8 32 56 2
[1] 5
[1] 16 8 32
[1] 16 56
[1] 16 8 32 56 2
[1] FALSE TRUE FALSE TRUE TRUE FALSE
[1] 16 32 56
x <- c(5,16,8,32,56,2)xx[1]x[2:4]x[c(2,5)]x[-1]x > 10x[x > 10]x[x >= 5 & x < 10]
[1] 5 16 8 32 56 2
[1] 5
[1] 16 8 32
[1] 16 56
[1] 16 8 32 56 2
[1] FALSE TRUE FALSE TRUE TRUE FALSE
[1] 16 32 56
[1] 5 8
x <- c(5,16,8,32,56,2)typeof(x)
[1] "double"
x <- c(5,16,8,32,56,2)typeof(x)is.vector(x)
[1] "double"
[1] TRUE
x <- c(5,16,8,32,56,2)typeof(x)is.vector(x)is.logical(x)
[1] "double"
[1] TRUE
[1] FALSE
x <- c(5,16,8,32,56,2)typeof(x)is.vector(x)is.logical(x)is.integer(x)
[1] "double"
[1] TRUE
[1] FALSE
[1] FALSE
a <- c(5,4,8)b <- c('a','b','c')h <- c(T,F,T)g <- c(1.5,2.5,3.7)
a <- c(5,4,8)b <- c('a','b','c')h <- c(T,F,T)g <- c(1.5,2.5,3.7)df <- data.frame(a,b,h)
a <- c(5,4,8)b <- c('a','b','c')h <- c(T,F,T)g <- c(1.5,2.5,3.7)df <- data.frame(a,b,h)df
a b h1 5 a TRUE2 4 b FALSE3 8 c TRUE
a <- c(5,4,8)b <- c('a','b','c')h <- c(T,F,T)g <- c(1.5,2.5,3.7)df <- data.frame(a,b,h)dfdf <- data.frame(a = c(5,4,8), b = c('a','b','c'), h = c(T,F,T), g = c(1.5,2.5,3.7))df
a b h1 5 a TRUE2 4 b FALSE3 8 c TRUE
a b h g1 5 a TRUE 1.52 4 b FALSE 2.53 8 c TRUE 3.7
a <- c(5,4,8)b <- c('a','b','c')h <- c(T,F,T)g <- c(1.5,2.5,3.7)df <- data.frame(a,b,h)dfdf <- data.frame(a = c(5,4,8), b = c('a','b','c'), h = c(T,F,T), g = c(1.5,2.5,3.7))dfstr(df)
a b h1 5 a TRUE2 4 b FALSE3 8 c TRUE
a b h g1 5 a TRUE 1.52 4 b FALSE 2.53 8 c TRUE 3.7
'data.frame': 3 obs. of 4 variables: $ a: num 5 4 8 $ b: Factor w/ 3 levels "a","b","c": 1 2 3 $ h: logi TRUE FALSE TRUE $ g: num 1.5 2.5 3.7
a <- c(5,4,8)b <- c('a','b','c')h <- c(T,F,T)g <- c(1.5,2.5,3.7)df <- data.frame(a,b,h)dfdf <- data.frame(a = c(5,4,8), b = c('a','b','c'), h = c(T,F,T), g = c(1.5,2.5,3.7))dfstr(df)dim(df)
a b h1 5 a TRUE2 4 b FALSE3 8 c TRUE
a b h g1 5 a TRUE 1.52 4 b FALSE 2.53 8 c TRUE 3.7
'data.frame': 3 obs. of 4 variables: $ a: num 5 4 8 $ b: Factor w/ 3 levels "a","b","c": 1 2 3 $ h: logi TRUE FALSE TRUE $ g: num 1.5 2.5 3.7
[1] 3 4
a <- c(5,4,8)b <- c('a','b','c')h <- c(T,F,T)g <- c(1.5,2.5,3.7)df <- data.frame(a,b,h)dfdf <- data.frame(a = c(5,4,8), b = c('a','b','c'), h = c(T,F,T), g = c(1.5,2.5,3.7))dfstr(df)dim(df)colnames(df)
a b h1 5 a TRUE2 4 b FALSE3 8 c TRUE
a b h g1 5 a TRUE 1.52 4 b FALSE 2.53 8 c TRUE 3.7
'data.frame': 3 obs. of 4 variables: $ a: num 5 4 8 $ b: Factor w/ 3 levels "a","b","c": 1 2 3 $ h: logi TRUE FALSE TRUE $ g: num 1.5 2.5 3.7
[1] 3 4
[1] "a" "b" "h" "g"
a <- c(5,4,8)b <- c('a','b','c')h <- c(T,F,T)g <- c(1.5,2.5,3.7)df <- data.frame(a,b,h)dfdf <- data.frame(a = c(5,4,8), b = c('a','b','c'), h = c(T,F,T), g = c(1.5,2.5,3.7))dfstr(df)dim(df)colnames(df)rownames(df)
a b h1 5 a TRUE2 4 b FALSE3 8 c TRUE
a b h g1 5 a TRUE 1.52 4 b FALSE 2.53 8 c TRUE 3.7
'data.frame': 3 obs. of 4 variables: $ a: num 5 4 8 $ b: Factor w/ 3 levels "a","b","c": 1 2 3 $ h: logi TRUE FALSE TRUE $ g: num 1.5 2.5 3.7
[1] 3 4
[1] "a" "b" "h" "g"
[1] "1" "2" "3"
a <- c(5,4,8)b <- c('a','b','c')h <- c(T,F,T)g <- c(1.5,2.5,3.7)df <- data.frame(a,b,h)dfdf <- data.frame(a = c(5,4,8), b = c('a','b','c'), h = c(T,F,T), g = c(1.5,2.5,3.7))dfstr(df)dim(df)colnames(df)rownames(df)head(df)
a b h1 5 a TRUE2 4 b FALSE3 8 c TRUE
a b h g1 5 a TRUE 1.52 4 b FALSE 2.53 8 c TRUE 3.7
'data.frame': 3 obs. of 4 variables: $ a: num 5 4 8 $ b: Factor w/ 3 levels "a","b","c": 1 2 3 $ h: logi TRUE FALSE TRUE $ g: num 1.5 2.5 3.7
[1] 3 4
[1] "a" "b" "h" "g"
[1] "1" "2" "3"
a b h g1 5 a TRUE 1.52 4 b FALSE 2.53 8 c TRUE 3.7
Сколько строк? Сколько столбцов?
data()
?mtcars
head(mtcars,4)
mpg cyl disp hp drat wt qsec vs am gear carbMazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
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 ...
colnames(mtcars)
[1] "mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear"[11] "carb"
table(mtcars$cyl)
4 6 8 11 7 14
dim(mtcars)
[1] 32 11
head(mtcars,4)
mpg cyl disp hp drat wt qsec vs am gear carbMazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
mtcars[3,6]
[1] 2.32
mtcars[8,]
mpg cyl disp hp drat wt qsec vs am gear carbMerc 240D 24.4 4 146.7 62 3.69 3.19 20 1 0 4 2
mtcars[1:3,]
mpg cyl disp hp drat wt qsec vs am gear carbMazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
mtcars['Mazda RX4',c('mpg','cyl')]
mpg cylMazda RX4 21 6
mtcars[mtcars$cyl > 4 & mtcars$cyl < 8,]
mpg cyl disp hp drat wt qsec vs am gear carbMazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6
table(mtcars$cyl)
4 6 8 11 7 14
table(mtcars$cyl)vec <- c(4,8)vec
4 6 8 11 7 14
[1] 4 8
table(mtcars$cyl)vec <- c(4,8)vecmt <- mtcars[mtcars$cyl %in% vec,]table(mt$cyl)
4 6 8 11 7 14
[1] 4 8
4 8 11 14
table(mtcars$cyl)vec <- c(4,8)vecmt <- mtcars[mtcars$cyl %in% vec,]table(mt$cyl)mt <- mtcars[!(mtcars$cyl %in% vec),]table(mt$cyl)
4 6 8 11 7 14
[1] 4 8
4 8 11 14
6 7
Жарикова Анастасия Александровна - azharikova89@gmail.com
Пензар Дмитрий Дмитриевич - dmitrypenzar1996@gmail.com
Валяева Анна Александровна - valyaeva.ann@gmail.com
Ходите на занятия
Чат ТГ "ФББ набор 2019, Алгоритмы и R"
Комната 633, корпус Б
Keyboard shortcuts
↑, ←, Pg Up, k | Go to previous slide |
↓, →, Pg Dn, Space, j | Go to next slide |
Home | Go to first slide |
End | Go to last slide |
Number + Return | Go to specific slide |
b / m / f | Toggle blackout / mirrored / fullscreen mode |
c | Clone slideshow |
p | Toggle presenter mode |
t | Restart the presentation timer |
?, h | Toggle this help |
Esc | Back to slideshow |