class: center, middle, inverse, title-slide .title[ # Язык R и его применение в биоинформатике ] .author[ ### Анастасия Жарикова ] .date[ ### 09.09.2022 ] --- <style> html { padding: unset;} body { padding: unset;} </style> # Markdown В RStudio: File -> New File -> R Markdown -> пример документа Запускается целиком, сразу видны ошибки и проблемы Фрагменты кода записывают в “chunk”: <img src="data:image/png;base64,#img/chunk.png" width="80%" style="display: block; margin: auto;" /> Переменные и функции, инициализированные в одном chunk, будут работать во всем документе --- # Markdown <img src="data:image/png;base64,#img/markdown.PNG" width="80%" style="display: block; margin: auto;" /> --- # Markdown В случае ошибки в коде итоговый документ не будет создан, появится сообщение об ошибке <img src="data:image/png;base64,#img/error.PNG" width="80%" style="display: block; margin: auto;" /> Читаем ошибку - исправляем - перезапускаем --- # Markdown
https://rmarkdown.rstudio.com/index.html https://www.rstudio.com/wp-content/uploads/2015/03/rmarkdown-reference.pdf ## Домашние задания: - делаем в markdown (file.Rmd) - сохраняем в file.html (или file.pdf) - прикрепляем в форму --- count: false # Вектор - данные одного типа .panel1-vec33-user[ ```r *x <- c(T,F,F,T) *typeof(x) ``` ] .panel2-vec33-user[ ``` [1] "logical" ``` ] --- count: false # Вектор - данные одного типа .panel1-vec33-user[ ```r x <- c(T,F,F,T) typeof(x) *x <- c(0.5, 1.2, 3.6) *typeof(x) ``` ] .panel2-vec33-user[ ``` [1] "logical" ``` ``` [1] "double" ``` ] --- count: false # Вектор - данные одного типа .panel1-vec33-user[ ```r x <- c(T,F,F,T) typeof(x) x <- c(0.5, 1.2, 3.6) typeof(x) *x <- 1:5 *typeof(x) ``` ] .panel2-vec33-user[ ``` [1] "logical" ``` ``` [1] "double" ``` ``` [1] "integer" ``` ] --- count: false # Вектор - данные одного типа .panel1-vec33-user[ ```r x <- c(T,F,F,T) typeof(x) x <- c(0.5, 1.2, 3.6) typeof(x) x <- 1:5 typeof(x) *x <- c('a','b','c') *typeof(x) ``` ] .panel2-vec33-user[ ``` [1] "logical" ``` ``` [1] "double" ``` ``` [1] "integer" ``` ``` [1] "character" ``` ] --- count: false # Вектор - данные одного типа .panel1-vec33-user[ ```r x <- c(T,F,F,T) typeof(x) x <- c(0.5, 1.2, 3.6) typeof(x) x <- 1:5 typeof(x) x <- c('a','b','c') typeof(x) *x <- c(F,1,2,T) *typeof(x) ``` ] .panel2-vec33-user[ ``` [1] "logical" ``` ``` [1] "double" ``` ``` [1] "integer" ``` ``` [1] "character" ``` ``` [1] "double" ``` ] --- count: false # Вектор - данные одного типа .panel1-vec33-user[ ```r x <- c(T,F,F,T) typeof(x) x <- c(0.5, 1.2, 3.6) typeof(x) x <- 1:5 typeof(x) x <- c('a','b','c') typeof(x) x <- c(F,1,2,T) typeof(x) *x ``` ] .panel2-vec33-user[ ``` [1] "logical" ``` ``` [1] "double" ``` ``` [1] "integer" ``` ``` [1] "character" ``` ``` [1] "double" ``` ``` [1] 0 1 2 1 ``` ] --- count: false # Вектор - данные одного типа .panel1-vec33-user[ ```r x <- c(T,F,F,T) typeof(x) x <- c(0.5, 1.2, 3.6) typeof(x) x <- 1:5 typeof(x) x <- c('a','b','c') typeof(x) x <- c(F,1,2,T) typeof(x) x *x <- c(1,2.8) *typeof(x) ``` ] .panel2-vec33-user[ ``` [1] "logical" ``` ``` [1] "double" ``` ``` [1] "integer" ``` ``` [1] "character" ``` ``` [1] "double" ``` ``` [1] 0 1 2 1 ``` ``` [1] "double" ``` ] --- count: false # Вектор - данные одного типа .panel1-vec33-user[ ```r x <- c(T,F,F,T) typeof(x) x <- c(0.5, 1.2, 3.6) typeof(x) x <- 1:5 typeof(x) x <- c('a','b','c') typeof(x) x <- c(F,1,2,T) typeof(x) x x <- c(1,2.8) typeof(x) *x ``` ] .panel2-vec33-user[ ``` [1] "logical" ``` ``` [1] "double" ``` ``` [1] "integer" ``` ``` [1] "character" ``` ``` [1] "double" ``` ``` [1] 0 1 2 1 ``` ``` [1] "double" ``` ``` [1] 1.0 2.8 ``` ] --- count: false # Вектор - данные одного типа .panel1-vec33-user[ ```r x <- c(T,F,F,T) typeof(x) x <- c(0.5, 1.2, 3.6) typeof(x) x <- 1:5 typeof(x) x <- c('a','b','c') typeof(x) x <- c(F,1,2,T) typeof(x) x x <- c(1,2.8) typeof(x) x *x <- c(1,'a',F,5.5) *typeof(x) ``` ] .panel2-vec33-user[ ``` [1] "logical" ``` ``` [1] "double" ``` ``` [1] "integer" ``` ``` [1] "character" ``` ``` [1] "double" ``` ``` [1] 0 1 2 1 ``` ``` [1] "double" ``` ``` [1] 1.0 2.8 ``` ``` [1] "character" ``` ] --- count: false # Вектор - данные одного типа .panel1-vec33-user[ ```r x <- c(T,F,F,T) typeof(x) x <- c(0.5, 1.2, 3.6) typeof(x) x <- 1:5 typeof(x) x <- c('a','b','c') typeof(x) x <- c(F,1,2,T) typeof(x) x x <- c(1,2.8) typeof(x) x x <- c(1,'a',F,5.5) typeof(x) *x ``` ] .panel2-vec33-user[ ``` [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" ``` ] <style> .panel1-vec33-user { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-vec33-user { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-vec33-user { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false # Срезы: по индексам .panel1-vec4-user[ ```r *x <- c(5,16,8,32,56,2) *x ``` ] .panel2-vec4-user[ ``` [1] 5 16 8 32 56 2 ``` ] --- count: false # Срезы: по индексам .panel1-vec4-user[ ```r x <- c(5,16,8,32,56,2) x *x[1] ``` ] .panel2-vec4-user[ ``` [1] 5 16 8 32 56 2 ``` ``` [1] 5 ``` ] --- count: false # Срезы: по индексам .panel1-vec4-user[ ```r x <- c(5,16,8,32,56,2) x x[1] *x[2:4] ``` ] .panel2-vec4-user[ ``` [1] 5 16 8 32 56 2 ``` ``` [1] 5 ``` ``` [1] 16 8 32 ``` ] --- count: false # Срезы: по индексам .panel1-vec4-user[ ```r x <- c(5,16,8,32,56,2) x x[1] x[2:4] *x[c(2,5)] ``` ] .panel2-vec4-user[ ``` [1] 5 16 8 32 56 2 ``` ``` [1] 5 ``` ``` [1] 16 8 32 ``` ``` [1] 16 56 ``` ] --- count: false # Срезы: по индексам .panel1-vec4-user[ ```r x <- c(5,16,8,32,56,2) x x[1] x[2:4] x[c(2,5)] *x[-1] ``` ] .panel2-vec4-user[ ``` [1] 5 16 8 32 56 2 ``` ``` [1] 5 ``` ``` [1] 16 8 32 ``` ``` [1] 16 56 ``` ``` [1] 16 8 32 56 2 ``` ] --- count: false # Срезы: по индексам .panel1-vec4-user[ ```r x <- c(5,16,8,32,56,2) x x[1] x[2:4] x[c(2,5)] x[-1] *x > 10 ``` ] .panel2-vec4-user[ ``` [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 ``` ] --- count: false # Срезы: по индексам .panel1-vec4-user[ ```r x <- c(5,16,8,32,56,2) x x[1] x[2:4] x[c(2,5)] x[-1] x > 10 *x[x > 10] ``` ] .panel2-vec4-user[ ``` [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 ``` ] --- count: false # Срезы: по индексам .panel1-vec4-user[ ```r x <- c(5,16,8,32,56,2) x x[1] x[2:4] x[c(2,5)] x[-1] x > 10 x[x > 10] *x[x >= 5 & x < 10] ``` ] .panel2-vec4-user[ ``` [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 ``` ] <style> .panel1-vec4-user { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-vec4-user { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-vec4-user { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style>
--- --- count: false # Data frame .panel1-vec6-user[ ```r *a <- c(5,4,8) *b <- c('a','b','c') *h <- c(T,F,T) *g <- c(1.5,2.5,3.7) ``` ] .panel2-vec6-user[ ] --- count: false # Data frame .panel1-vec6-user[ ```r 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) ``` ] .panel2-vec6-user[ ] --- count: false # Data frame .panel1-vec6-user[ ```r 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 ``` ] .panel2-vec6-user[ ``` a b h 1 5 a TRUE 2 4 b FALSE 3 8 c TRUE ``` ] --- count: false # Data frame .panel1-vec6-user[ ```r 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 *df <- 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 ``` ] .panel2-vec6-user[ ``` a b h 1 5 a TRUE 2 4 b FALSE 3 8 c TRUE ``` ``` a b h g 1 5 a TRUE 1.5 2 4 b FALSE 2.5 3 8 c TRUE 3.7 ``` ] --- count: false # Data frame .panel1-vec6-user[ ```r 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 df <- 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 *str(df) ``` ] .panel2-vec6-user[ ``` a b h 1 5 a TRUE 2 4 b FALSE 3 8 c TRUE ``` ``` a b h g 1 5 a TRUE 1.5 2 4 b FALSE 2.5 3 8 c TRUE 3.7 ``` ``` 'data.frame': 3 obs. of 4 variables: $ a: num 5 4 8 $ b: chr "a" "b" "c" $ h: logi TRUE FALSE TRUE $ g: num 1.5 2.5 3.7 ``` ] --- count: false # Data frame .panel1-vec6-user[ ```r 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 df <- 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 str(df) *dim(df) ``` ] .panel2-vec6-user[ ``` a b h 1 5 a TRUE 2 4 b FALSE 3 8 c TRUE ``` ``` a b h g 1 5 a TRUE 1.5 2 4 b FALSE 2.5 3 8 c TRUE 3.7 ``` ``` 'data.frame': 3 obs. of 4 variables: $ a: num 5 4 8 $ b: chr "a" "b" "c" $ h: logi TRUE FALSE TRUE $ g: num 1.5 2.5 3.7 ``` ``` [1] 3 4 ``` ] --- count: false # Data frame .panel1-vec6-user[ ```r 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 df <- 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 str(df) dim(df) *colnames(df) ``` ] .panel2-vec6-user[ ``` a b h 1 5 a TRUE 2 4 b FALSE 3 8 c TRUE ``` ``` a b h g 1 5 a TRUE 1.5 2 4 b FALSE 2.5 3 8 c TRUE 3.7 ``` ``` 'data.frame': 3 obs. of 4 variables: $ a: num 5 4 8 $ b: chr "a" "b" "c" $ h: logi TRUE FALSE TRUE $ g: num 1.5 2.5 3.7 ``` ``` [1] 3 4 ``` ``` [1] "a" "b" "h" "g" ``` ] --- count: false # Data frame .panel1-vec6-user[ ```r 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 df <- 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 str(df) dim(df) colnames(df) *rownames(df) ``` ] .panel2-vec6-user[ ``` a b h 1 5 a TRUE 2 4 b FALSE 3 8 c TRUE ``` ``` a b h g 1 5 a TRUE 1.5 2 4 b FALSE 2.5 3 8 c TRUE 3.7 ``` ``` 'data.frame': 3 obs. of 4 variables: $ a: num 5 4 8 $ b: chr "a" "b" "c" $ 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" ``` ] --- count: false # Data frame .panel1-vec6-user[ ```r 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 df <- 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 str(df) dim(df) colnames(df) rownames(df) *head(df) ``` ] .panel2-vec6-user[ ``` a b h 1 5 a TRUE 2 4 b FALSE 3 8 c TRUE ``` ``` a b h g 1 5 a TRUE 1.5 2 4 b FALSE 2.5 3 8 c TRUE 3.7 ``` ``` 'data.frame': 3 obs. of 4 variables: $ a: num 5 4 8 $ b: chr "a" "b" "c" $ 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 g 1 5 a TRUE 1.5 2 4 b FALSE 2.5 3 8 c TRUE 3.7 ``` ] <style> .panel1-vec6-user { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-vec6-user { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-vec6-user { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> Сколько строк? Сколько столбцов?
--- # Встроенные наборы данных data() ?mtcars --- # mtcars
```r head(mtcars,4) ``` ``` mpg cyl disp hp drat wt qsec vs am gear carb Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 ``` ```r 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 ... ``` --- # mtcars
```r colnames(mtcars) ``` ``` [1] "mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear" [11] "carb" ``` ```r table(mtcars$cyl) ``` ``` 4 6 8 11 7 14 ``` --- # Data frame - Срезы
```r dim(mtcars) ``` ``` [1] 32 11 ``` ```r head(mtcars,4) ``` ``` mpg cyl disp hp drat wt qsec vs am gear carb Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 ``` ```r mtcars[3,6] ``` ``` [1] 2.32 ``` --- # Data frame - Срезы
```r mtcars[8,] ``` ``` mpg cyl disp hp drat wt qsec vs am gear carb Merc 240D 24.4 4 146.7 62 3.69 3.19 20 1 0 4 2 ``` ```r mtcars[1:3,] ``` ``` mpg cyl disp hp drat wt qsec vs am gear carb Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 ``` ```r mtcars['Mazda RX4',c('mpg','cyl')] ``` ``` mpg cyl Mazda RX4 21 6 ``` --- # Data frame - Выбор данных по условию
```r mtcars[mtcars$cyl > 4 & mtcars$cyl < 8,] ``` ``` 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 Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1 Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1 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 Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6 ``` --- count: false # Data frame - Выбор данных по условию .panel1-vec7-user[ ```r *table(mtcars$cyl) ``` ] .panel2-vec7-user[ ``` 4 6 8 11 7 14 ``` ] --- count: false # Data frame - Выбор данных по условию .panel1-vec7-user[ ```r table(mtcars$cyl) *vec <- c(4,8) *vec ``` ] .panel2-vec7-user[ ``` 4 6 8 11 7 14 ``` ``` [1] 4 8 ``` ] --- count: false # Data frame - Выбор данных по условию .panel1-vec7-user[ ```r table(mtcars$cyl) vec <- c(4,8) vec *mt <- mtcars[mtcars$cyl %in% vec,] *table(mt$cyl) ``` ] .panel2-vec7-user[ ``` 4 6 8 11 7 14 ``` ``` [1] 4 8 ``` ``` 4 8 11 14 ``` ] --- count: false # Data frame - Выбор данных по условию .panel1-vec7-user[ ```r table(mtcars$cyl) vec <- c(4,8) vec mt <- mtcars[mtcars$cyl %in% vec,] table(mt$cyl) *mt <- mtcars[!(mtcars$cyl %in% vec),] *table(mt$cyl) ``` ] .panel2-vec7-user[ ``` 4 6 8 11 7 14 ``` ``` [1] 4 8 ``` ``` 4 8 11 14 ``` ``` 6 7 ``` ] <style> .panel1-vec7-user { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-vec7-user { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-vec7-user { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style>
--- count: false # Пример Создайте вектор х, содержащий цифры от 1 до 5 включительно. Возведите элементы вектора х в квадрат. Посчитайте сумму элементов получившегося вектора. .panel1-vec-user[ ] .panel2-vec-user[ ] --- count: false # Пример Создайте вектор х, содержащий цифры от 1 до 5 включительно. Возведите элементы вектора х в квадрат. Посчитайте сумму элементов получившегося вектора. .panel1-vec-user[ ```r *x <- 1:5 *x ``` ] .panel2-vec-user[ ``` [1] 1 2 3 4 5 ``` ] --- count: false # Пример Создайте вектор х, содержащий цифры от 1 до 5 включительно. Возведите элементы вектора х в квадрат. Посчитайте сумму элементов получившегося вектора. .panel1-vec-user[ ```r x <- 1:5 x *x^2 ``` ] .panel2-vec-user[ ``` [1] 1 2 3 4 5 ``` ``` [1] 1 4 9 16 25 ``` ] --- count: false # Пример Создайте вектор х, содержащий цифры от 1 до 5 включительно. Возведите элементы вектора х в квадрат. Посчитайте сумму элементов получившегося вектора. .panel1-vec-user[ ```r x <- 1:5 x x^2 *sum(x^2) ``` ] .panel2-vec-user[ ``` [1] 1 2 3 4 5 ``` ``` [1] 1 4 9 16 25 ``` ``` [1] 55 ``` ] --- count: false # Пример Создайте вектор х, содержащий цифры от 1 до 5 включительно. Возведите элементы вектора х в квадрат. Посчитайте сумму элементов получившегося вектора. .panel1-vec-user[ ```r x <- 1:5 x x^2 sum(x^2) *y=x^2 *sum(y) ``` ] .panel2-vec-user[ ``` [1] 1 2 3 4 5 ``` ``` [1] 1 4 9 16 25 ``` ``` [1] 55 ``` ``` [1] 55 ``` ] <style> .panel1-vec-user { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-vec-user { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-vec-user { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style>
Не забывайте результат ваших манипуляций положить в переменную, если это нужно! --- count: false # which .panel1-vec23-user[ ```r *mtcars[1:3,1:3] ``` ] .panel2-vec23-user[ ``` mpg cyl disp Mazda RX4 21.0 6 160 Mazda RX4 Wag 21.0 6 160 Datsun 710 22.8 4 108 ``` ] --- count: false # which .panel1-vec23-user[ ```r mtcars[1:3,1:3] *which(mtcars$mpg == 21) ``` ] .panel2-vec23-user[ ``` mpg cyl disp Mazda RX4 21.0 6 160 Mazda RX4 Wag 21.0 6 160 Datsun 710 22.8 4 108 ``` ``` [1] 1 2 ``` ] --- count: false # which .panel1-vec23-user[ ```r mtcars[1:3,1:3] which(mtcars$mpg == 21) *which(colnames(mtcars) == 'disp') ``` ] .panel2-vec23-user[ ``` mpg cyl disp Mazda RX4 21.0 6 160 Mazda RX4 Wag 21.0 6 160 Datsun 710 22.8 4 108 ``` ``` [1] 1 2 ``` ``` [1] 3 ``` ] --- count: false # which .panel1-vec23-user[ ```r mtcars[1:3,1:3] which(mtcars$mpg == 21) which(colnames(mtcars) == 'disp') *which.min(mtcars$qsec) ``` ] .panel2-vec23-user[ ``` mpg cyl disp Mazda RX4 21.0 6 160 Mazda RX4 Wag 21.0 6 160 Datsun 710 22.8 4 108 ``` ``` [1] 1 2 ``` ``` [1] 3 ``` ``` [1] 29 ``` ] --- count: false # which .panel1-vec23-user[ ```r mtcars[1:3,1:3] which(mtcars$mpg == 21) which(colnames(mtcars) == 'disp') which.min(mtcars$qsec) *which.max(mtcars$qsec) ``` ] .panel2-vec23-user[ ``` mpg cyl disp Mazda RX4 21.0 6 160 Mazda RX4 Wag 21.0 6 160 Datsun 710 22.8 4 108 ``` ``` [1] 1 2 ``` ``` [1] 3 ``` ``` [1] 29 ``` ``` [1] 9 ``` ] --- count: false # which .panel1-vec23-user[ ```r mtcars[1:3,1:3] which(mtcars$mpg == 21) which(colnames(mtcars) == 'disp') which.min(mtcars$qsec) which.max(mtcars$qsec) ``` ] .panel2-vec23-user[ ``` mpg cyl disp Mazda RX4 21.0 6 160 Mazda RX4 Wag 21.0 6 160 Datsun 710 22.8 4 108 ``` ``` [1] 1 2 ``` ``` [1] 3 ``` ``` [1] 29 ``` ``` [1] 9 ``` ] <style> .panel1-vec23-user { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-vec23-user { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-vec23-user { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- # Сортировка
```r mtcars[order(mtcars$qsec),] ``` ``` mpg cyl disp hp drat wt qsec vs am gear carb 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 Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4 Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6 Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4 Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4 Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2 Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2 Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2 Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4 Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2 Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2 AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2 Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3 Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4 Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3 Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4 Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4 Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3 Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4 Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2 Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2 Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4 Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1 Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1 Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1 Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1 Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2 Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1 Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1 Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2 ``` --- # Округление
```r x <- c(1,3,5,9,2,3) mean(x) ``` ``` [1] 3.833333 ``` ```r round(mean(x),3) ``` ``` [1] 3.833 ``` ```r round(mean(x),1) ``` ``` [1] 3.8 ``` Не нужно указывать много знаков после запятой, если это не несет глубокого смысла --- # Работа с переменными
```r ls() ``` ``` [1] "a" "b" "df" "g" "h" "mt" "vec" "x" "y" ``` ```r rm(list=ls()) ls() ``` ``` character(0) ``` --- # Сохранение данных
```r write.table(mtcars,file='mtnew.tab',quote=T,col.names = T,row.names=T,sep='\t') write.csv(mtcars,file='mtnew.csv') save(mtcars,file='mtnew.RData') ``` --- # Чтение данных
```r mt <- read.table('mtnew.tab',sep='\t',header=T) head(mt) ``` ``` mpg cyl disp hp drat wt qsec vs am gear carb Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1 ``` --- # Чтение данных
```r mt <- read.table('mtnew.tab',skip=2) head(mt) ``` ``` V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 1 Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 2 Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 3 Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 4 Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 5 Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1 6 Duster 360 14.3 8 360 245 3.21 3.570 15.84 0 0 3 4 ``` --- # Чтение данных
```r mt <- read.table('mtnew.tab',skip=2, header=T) head(mt) ``` ``` Mazda.RX4.Wag X21 X6 X160 X110 X3.9 X2.875 X17.02 X0 X1 X4 X4.1 1 Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 2 Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1 3 Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2 4 Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1 5 Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4 6 Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2 ``` --- # RData
```r rm(list=ls()) ls() ``` ``` character(0) ``` ```r load('mtnew.RData') ls() ``` ``` [1] "mtcars" ``` --- count: false # Данные о качестве воздуха .panel1-vec32-user[ ```r *head(airquality) ``` ] .panel2-vec32-user[ ``` Ozone Solar.R Wind Temp Month Day 1 41 190 7.4 67 5 1 2 36 118 8.0 72 5 2 3 12 149 12.6 74 5 3 4 18 313 11.5 62 5 4 5 NA NA 14.3 56 5 5 6 28 NA 14.9 66 5 6 ``` ] --- count: false # Данные о качестве воздуха .panel1-vec32-user[ ```r head(airquality) *dim(airquality) ``` ] .panel2-vec32-user[ ``` Ozone Solar.R Wind Temp Month Day 1 41 190 7.4 67 5 1 2 36 118 8.0 72 5 2 3 12 149 12.6 74 5 3 4 18 313 11.5 62 5 4 5 NA NA 14.3 56 5 5 6 28 NA 14.9 66 5 6 ``` ``` [1] 153 6 ``` ] --- count: false # Данные о качестве воздуха .panel1-vec32-user[ ```r head(airquality) dim(airquality) *mean(airquality$Ozone) ``` ] .panel2-vec32-user[ ``` Ozone Solar.R Wind Temp Month Day 1 41 190 7.4 67 5 1 2 36 118 8.0 72 5 2 3 12 149 12.6 74 5 3 4 18 313 11.5 62 5 4 5 NA NA 14.3 56 5 5 6 28 NA 14.9 66 5 6 ``` ``` [1] 153 6 ``` ``` [1] NA ``` ] <style> .panel1-vec32-user { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-vec32-user { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-vec32-user { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> В чем проблема? --- # Работа с отсутствующими данными
- NA - пропущенное значение: is.na() - NaN - результат недопустимой арифметической операции: is.nan() - NULL - отсутствие субъекта: is.null() --- # Работа с отсутствующими данными - Не учитывать отсутствующие данные ```r mean(airquality$Ozone, na.rm = T) ``` ``` [1] 42.12931 ``` --- count: false # Работа с отсутствующими данными Удалить строки с отсутствующими данными Удаляется ВСЯ строка! .panel1-vec55-user[ ```r *dim(airquality) *air <- na.omit(airquality) ``` ] .panel2-vec55-user[ ``` [1] 153 6 ``` ] --- count: false # Работа с отсутствующими данными Удалить строки с отсутствующими данными Удаляется ВСЯ строка! .panel1-vec55-user[ ```r dim(airquality) air <- na.omit(airquality) *dim(air) ``` ] .panel2-vec55-user[ ``` [1] 153 6 ``` ``` [1] 111 6 ``` ] --- count: false # Работа с отсутствующими данными Удалить строки с отсутствующими данными Удаляется ВСЯ строка! .panel1-vec55-user[ ```r dim(airquality) air <- na.omit(airquality) dim(air) *mean(air$Ozone) ``` ] .panel2-vec55-user[ ``` [1] 153 6 ``` ``` [1] 111 6 ``` ``` [1] 42.0991 ``` ] --- count: false # Работа с отсутствующими данными Удалить строки с отсутствующими данными Удаляется ВСЯ строка! .panel1-vec55-user[ ```r dim(airquality) air <- na.omit(airquality) dim(air) mean(air$Ozone) *mean(airquality$Ozone, na.rm = T) ``` ] .panel2-vec55-user[ ``` [1] 153 6 ``` ``` [1] 111 6 ``` ``` [1] 42.0991 ``` ``` [1] 42.12931 ``` ] <style> .panel1-vec55-user { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-vec55-user { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-vec55-user { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false # Работа с отсутствующими данными .panel1-vec65-user[ ```r *air <- head(airquality,10) *is.na(air$Ozone) ``` ] .panel2-vec65-user[ ``` [1] FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE TRUE ``` ] --- count: false # Работа с отсутствующими данными .panel1-vec65-user[ ```r air <- head(airquality,10) is.na(air$Ozone) *sum(is.na(air$Ozone)) ``` ] .panel2-vec65-user[ ``` [1] FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE TRUE ``` ``` [1] 2 ``` ] --- count: false # Работа с отсутствующими данными .panel1-vec65-user[ ```r air <- head(airquality,10) is.na(air$Ozone) sum(is.na(air$Ozone)) *which(is.na(air$Ozone)) ``` ] .panel2-vec65-user[ ``` [1] FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE TRUE ``` ``` [1] 2 ``` ``` [1] 5 10 ``` ] --- count: false # Работа с отсутствующими данными .panel1-vec65-user[ ```r air <- head(airquality,10) is.na(air$Ozone) sum(is.na(air$Ozone)) which(is.na(air$Ozone)) *anyNA(air$Ozone) ``` ] .panel2-vec65-user[ ``` [1] FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE TRUE ``` ``` [1] 2 ``` ``` [1] 5 10 ``` ``` [1] TRUE ``` ] <style> .panel1-vec65-user { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-vec65-user { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-vec65-user { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- # NaN
```r 0/0 ``` ``` [1] NaN ``` --- # Удаление столбцов
```r head(air,3) ``` ``` Ozone Solar.R Wind Temp Month Day 1 41 190 7.4 67 5 1 2 36 118 8.0 72 5 2 3 12 149 12.6 74 5 3 ``` ```r air$Ozone = NULL head(air,3) ``` ``` Solar.R Wind Temp Month Day 1 190 7.4 67 5 1 2 118 8.0 72 5 2 3 149 12.6 74 5 3 ``` --- # Факторы ```r df <- data.frame(a = c(5,4,8,5,7,2), b = c('a','b','c','a','a','c'), h = c(T,F,T,F,T,T), g = c(1.5,2.5,3.7,6.9,4.2,4.6)) df ``` ``` a b h g 1 5 a TRUE 1.5 2 4 b FALSE 2.5 3 8 c TRUE 3.7 4 5 a FALSE 6.9 5 7 a TRUE 4.2 6 2 c TRUE 4.6 ``` --- # Факторы ```r str(df) ``` ``` 'data.frame': 6 obs. of 4 variables: $ a: num 5 4 8 5 7 2 $ b: chr "a" "b" "c" "a" ... $ h: logi TRUE FALSE TRUE FALSE TRUE TRUE $ g: num 1.5 2.5 3.7 6.9 4.2 4.6 ``` ```r levels(df$b) ``` ``` NULL ``` В read.table() и в data.frame() есть опция *stringsAsFactors = F* --- # Данные - Числовые -- Непрерывные (возраст, вес) -- Дискретные (количество домашних животных) - Категориальные -- Порядковые (имеют иерархию: уровень настроения, оценки) -- Номинальные (не имеют иерархии: цвет глаз, порода собак) --- При всяческих манипуляциях проверяйте, что происходит с данными:
- head(), tail() - dim() - length() - ncol(), nrow() - colnames(), rownames() - str() - unique() - ls() - anyNA() --- # Матрицы - Внешне похожи на data.frame - Вектор с атрибутом dim - Все элементы одного типа - Математические операции работают быстрее --- count: false # Способы создания матрицы .panel1-vec76-user[ ```r *m1 <- matrix(1:6, nrow=2, ncol=3) *m1 ``` ] .panel2-vec76-user[ ``` [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 ``` ] --- count: false # Способы создания матрицы .panel1-vec76-user[ ```r m1 <- matrix(1:6, nrow=2, ncol=3) m1 *m2 <- matrix(1:6, nrow=2, ncol=3,byrow=T) *m2 ``` ] .panel2-vec76-user[ ``` [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 ``` ``` [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 ``` ] --- count: false # Способы создания матрицы .panel1-vec76-user[ ```r m1 <- matrix(1:6, nrow=2, ncol=3) m1 m2 <- matrix(1:6, nrow=2, ncol=3,byrow=T) m2 *as.matrix(1:3) ``` ] .panel2-vec76-user[ ``` [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 ``` ``` [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 ``` ``` [,1] [1,] 1 [2,] 2 [3,] 3 ``` ] --- count: false # Способы создания матрицы .panel1-vec76-user[ ```r m1 <- matrix(1:6, nrow=2, ncol=3) m1 m2 <- matrix(1:6, nrow=2, ncol=3,byrow=T) m2 as.matrix(1:3) *v <- 1:6 *dim(v) ``` ] .panel2-vec76-user[ ``` [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 ``` ``` [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 ``` ``` [,1] [1,] 1 [2,] 2 [3,] 3 ``` ``` NULL ``` ] --- count: false # Способы создания матрицы .panel1-vec76-user[ ```r m1 <- matrix(1:6, nrow=2, ncol=3) m1 m2 <- matrix(1:6, nrow=2, ncol=3,byrow=T) m2 as.matrix(1:3) v <- 1:6 dim(v) *dim(v) = c(2,3) *v ``` ] .panel2-vec76-user[ ``` [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 ``` ``` [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 ``` ``` [,1] [1,] 1 [2,] 2 [3,] 3 ``` ``` NULL ``` ``` [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 ``` ] --- count: false # Способы создания матрицы .panel1-vec76-user[ ```r m1 <- matrix(1:6, nrow=2, ncol=3) m1 m2 <- matrix(1:6, nrow=2, ncol=3,byrow=T) m2 as.matrix(1:3) v <- 1:6 dim(v) dim(v) = c(2,3) v *is.matrix(v) ``` ] .panel2-vec76-user[ ``` [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 ``` ``` [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 ``` ``` [,1] [1,] 1 [2,] 2 [3,] 3 ``` ``` NULL ``` ``` [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 ``` ``` [1] TRUE ``` ] <style> .panel1-vec76-user { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-vec76-user { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-vec76-user { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style>
--- count: false # Способы создания матрицы .panel1-vec87-user[ ```r *a <- c(1,2,3) *b <- c(4,5,6) ``` ] .panel2-vec87-user[ ] --- count: false # Способы создания матрицы .panel1-vec87-user[ ```r a <- c(1,2,3) b <- c(4,5,6) *rbind(a,b) ``` ] .panel2-vec87-user[ ``` [,1] [,2] [,3] a 1 2 3 b 4 5 6 ``` ] --- count: false # Способы создания матрицы .panel1-vec87-user[ ```r a <- c(1,2,3) b <- c(4,5,6) rbind(a,b) *cbind(a,b) ``` ] .panel2-vec87-user[ ``` [,1] [,2] [,3] a 1 2 3 b 4 5 6 ``` ``` a b [1,] 1 4 [2,] 2 5 [3,] 3 6 ``` ] --- count: false # Способы создания матрицы .panel1-vec87-user[ ```r a <- c(1,2,3) b <- c(4,5,6) rbind(a,b) cbind(a,b) ``` ] .panel2-vec87-user[ ``` [,1] [,2] [,3] a 1 2 3 b 4 5 6 ``` ``` a b [1,] 1 4 [2,] 2 5 [3,] 3 6 ``` ] <style> .panel1-vec87-user { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-vec87-user { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-vec87-user { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style>
rbind() и cbind() работает и для data.frame --- count: false # Матрицы Срезы .panel1-vec9-user[ ```r *m1 <- matrix(1:6, nrow=2, ncol=3) *m1 ``` ] .panel2-vec9-user[ ``` [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 ``` ] --- count: false # Матрицы Срезы .panel1-vec9-user[ ```r m1 <- matrix(1:6, nrow=2, ncol=3) m1 *m1[2,3] ``` ] .panel2-vec9-user[ ``` [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 ``` ``` [1] 6 ``` ] --- count: false # Матрицы Срезы .panel1-vec9-user[ ```r m1 <- matrix(1:6, nrow=2, ncol=3) m1 m1[2,3] *m1[2,] ``` ] .panel2-vec9-user[ ``` [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 ``` ``` [1] 6 ``` ``` [1] 2 4 6 ``` ] --- count: false # Матрицы Срезы .panel1-vec9-user[ ```r m1 <- matrix(1:6, nrow=2, ncol=3) m1 m1[2,3] m1[2,] *m1[,3] ``` ] .panel2-vec9-user[ ``` [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 ``` ``` [1] 6 ``` ``` [1] 2 4 6 ``` ``` [1] 5 6 ``` ] --- count: false # Матрицы Срезы .panel1-vec9-user[ ```r m1 <- matrix(1:6, nrow=2, ncol=3) m1 m1[2,3] m1[2,] m1[,3] ``` ] .panel2-vec9-user[ ``` [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 ``` ``` [1] 6 ``` ``` [1] 2 4 6 ``` ``` [1] 5 6 ``` ] <style> .panel1-vec9-user { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-vec9-user { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-vec9-user { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style>
--- count: false # Матрицы Изменение значений .panel1-vec10-user[ ```r *m1 <- matrix(1:6, nrow=2, ncol=3) *m1 ``` ] .panel2-vec10-user[ ``` [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 ``` ] --- count: false # Матрицы Изменение значений .panel1-vec10-user[ ```r m1 <- matrix(1:6, nrow=2, ncol=3) m1 *m1[2,3] = 9 ``` ] .panel2-vec10-user[ ``` [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 ``` ] --- count: false # Матрицы Изменение значений .panel1-vec10-user[ ```r m1 <- matrix(1:6, nrow=2, ncol=3) m1 m1[2,3] = 9 *m1 ``` ] .panel2-vec10-user[ ``` [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 ``` ``` [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 9 ``` ] <style> .panel1-vec10-user { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-vec10-user { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-vec10-user { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style>
--- # Матрицы - Создать пустую матрицу ```r m1 <- matrix(nrow=2, ncol=3) m1 ``` ``` [,1] [,2] [,3] [1,] NA NA NA [2,] NA NA NA ``` --- # Матрицы ```r m1 <- matrix(1:6,nrow=2, ncol=3) m1 ``` ``` [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 ``` ```r rownames(m1) = c('r1','r2') colnames(m1) = c('c1','c2','c3') m1 ``` ``` c1 c2 c3 r1 1 3 5 r2 2 4 6 ``` Имена строк и столбцов должны быть уникальными!