class: center, middle, inverse, title-slide # Язык R и его применение в биоинформатике ### Анастасия Жарикова ### 10.09.2021 --- Как дела с домашним заданием? Получилось разобраться с RStudio? Умеем сохранять файлы типа script.R? Разобрались с markdown? Сложный ли квиз? --- 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-vec2-user[ ```r *mtcars[1:3,1:3] ``` ] .panel2-vec2-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-vec2-user[ ```r mtcars[1:3,1:3] *which(mtcars$mpg == 21) ``` ] .panel2-vec2-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-vec2-user[ ```r mtcars[1:3,1:3] which(mtcars$mpg == 21) *which(colnames(mtcars) == 'disp') ``` ] .panel2-vec2-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-vec2-user[ ```r mtcars[1:3,1:3] which(mtcars$mpg == 21) which(colnames(mtcars) == 'disp') *which.min(mtcars$qsec) ``` ] .panel2-vec2-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-vec2-user[ ```r mtcars[1:3,1:3] which(mtcars$mpg == 21) which(colnames(mtcars) == 'disp') which.min(mtcars$qsec) *which.max(mtcars$qsec) ``` ] .panel2-vec2-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-vec2-user[ ```r mtcars[1:3,1:3] which(mtcars$mpg == 21) which(colnames(mtcars) == 'disp') which.min(mtcars$qsec) which.max(mtcars$qsec) ``` ] .panel2-vec2-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-vec2-user { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-vec2-user { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-vec2-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] "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-vec3-user[ ```r *head(airquality) ``` ] .panel2-vec3-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-vec3-user[ ```r head(airquality) *dim(airquality) ``` ] .panel2-vec3-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-vec3-user[ ```r head(airquality) dim(airquality) *mean(airquality$Ozone) ``` ] .panel2-vec3-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-vec3-user { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-vec3-user { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-vec3-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() --- count: false # Работа с отсутствующими данными Не учитывать отсутствующие данные .panel1-vec4-user[ ```r *mean(airquality$Ozone, na.rm = T) ``` ] .panel2-vec4-user[ ``` [1] 42.12931 ``` ] <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 # Работа с отсутствующими данными Удалить строки с отсутствующими данными Удаляется ВСЯ строка! .panel1-vec5-user[ ```r *dim(airquality) *air <- na.omit(airquality) ``` ] .panel2-vec5-user[ ``` [1] 153 6 ``` ] --- count: false # Работа с отсутствующими данными Удалить строки с отсутствующими данными Удаляется ВСЯ строка! .panel1-vec5-user[ ```r dim(airquality) air <- na.omit(airquality) *dim(air) ``` ] .panel2-vec5-user[ ``` [1] 153 6 ``` ``` [1] 111 6 ``` ] --- count: false # Работа с отсутствующими данными Удалить строки с отсутствующими данными Удаляется ВСЯ строка! .panel1-vec5-user[ ```r dim(airquality) air <- na.omit(airquality) dim(air) *mean(air$Ozone) ``` ] .panel2-vec5-user[ ``` [1] 153 6 ``` ``` [1] 111 6 ``` ``` [1] 42.0991 ``` ] --- count: false # Работа с отсутствующими данными Удалить строки с отсутствующими данными Удаляется ВСЯ строка! .panel1-vec5-user[ ```r dim(airquality) air <- na.omit(airquality) dim(air) mean(air$Ozone) *mean(airquality$Ozone, na.rm = T) ``` ] .panel2-vec5-user[ ``` [1] 153 6 ``` ``` [1] 111 6 ``` ``` [1] 42.0991 ``` ``` [1] 42.12931 ``` ] <style> .panel1-vec5-user { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-vec5-user { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-vec5-user { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false # Работа с отсутствующими данными .panel1-vec6-user[ ```r *air <- head(airquality,10) *is.na(air$Ozone) ``` ] .panel2-vec6-user[ ``` [1] FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE TRUE ``` ] --- count: false # Работа с отсутствующими данными .panel1-vec6-user[ ```r air <- head(airquality,10) is.na(air$Ozone) *sum(is.na(air$Ozone)) ``` ] .panel2-vec6-user[ ``` [1] FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE TRUE ``` ``` [1] 2 ``` ] --- count: false # Работа с отсутствующими данными .panel1-vec6-user[ ```r air <- head(airquality,10) is.na(air$Ozone) sum(is.na(air$Ozone)) *which(is.na(air$Ozone)) ``` ] .panel2-vec6-user[ ``` [1] FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE TRUE ``` ``` [1] 2 ``` ``` [1] 5 10 ``` ] --- count: false # Работа с отсутствующими данными .panel1-vec6-user[ ```r air <- head(airquality,10) is.na(air$Ozone) sum(is.na(air$Ozone)) which(is.na(air$Ozone)) *anyNA(air$Ozone) ``` ] .panel2-vec6-user[ ``` [1] FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE TRUE ``` ``` [1] 2 ``` ``` [1] 5 10 ``` ``` [1] TRUE ``` ] <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> --- # 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: Factor w/ 3 levels "a","b","c": 1 2 3 1 1 3 $ 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) ``` ``` [1] "a" "b" "c" ``` В read.table() и в data.frame() есть опция *stringsAsFactors = F* --- # Данные - Числовые -- Непрерывные (возраст, вес) -- Дискретные (количество домашних животных) - Категориальные -- Порядковые (имеют иерархию: уровень настроения, оценки) -- Номинальные (не имеют иерархии: цвет глаз, порода собак) --- При всяческих манипуляциях проверяйте, что происходит с данными:
- head(), tail() - dim() - length() - ncol(), nrow() - colnames(), rownames() - str() - unique() - ls() - anyNA() --- # Матрицы - Внешне похожи на data.frame - Вектор с атрибутом dim - Все элементы одного типа - Математические операции работают быстрее --- count: false # Способы создания матрицы .panel1-vec7-user[ ```r *m1 <- matrix(1:6, nrow=2, ncol=3) *m1 ``` ] .panel2-vec7-user[ ``` [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 ``` ] --- count: false # Способы создания матрицы .panel1-vec7-user[ ```r m1 <- matrix(1:6, nrow=2, ncol=3) m1 *m2 <- matrix(1:6, nrow=2, ncol=3,byrow=T) *m2 ``` ] .panel2-vec7-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-vec7-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-vec7-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-vec7-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-vec7-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-vec7-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-vec7-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-vec7-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-vec7-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-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 # Способы создания матрицы .panel1-vec8-user[ ```r *a <- c(1,2,3) *b <- c(4,5,6) ``` ] .panel2-vec8-user[ ] --- count: false # Способы создания матрицы .panel1-vec8-user[ ```r a <- c(1,2,3) b <- c(4,5,6) *rbind(a,b) ``` ] .panel2-vec8-user[ ``` [,1] [,2] [,3] a 1 2 3 b 4 5 6 ``` ] --- count: false # Способы создания матрицы .panel1-vec8-user[ ```r a <- c(1,2,3) b <- c(4,5,6) rbind(a,b) *cbind(a,b) ``` ] .panel2-vec8-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-vec8-user[ ```r a <- c(1,2,3) b <- c(4,5,6) rbind(a,b) cbind(a,b) ``` ] .panel2-vec8-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-vec8-user { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-vec8-user { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-vec8-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>
--- count: false # Матрицы Создать пустую матрицу .panel1-vec11-user[ ```r *m1 <- matrix(nrow=2, ncol=3) *m1 ``` ] .panel2-vec11-user[ ``` [,1] [,2] [,3] [1,] NA NA NA [2,] NA NA NA ``` ] <style> .panel1-vec11-user { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-vec11-user { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-vec11-user { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style>
--- count: false # Матрицы .panel1-vec12-user[ ```r *m1 <- matrix(1:6,nrow=2, ncol=3) *m1 *rownames(m1) = c('r1','r2') *colnames(m1) = c('c1','c2','c3') *m1 ``` ] .panel2-vec12-user[ ``` [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 ``` ``` c1 c2 c3 r1 1 3 5 r2 2 4 6 ``` ] <style> .panel1-vec12-user { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-vec12-user { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-vec12-user { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style>
Имена строк и столбцов должны быть уникальными! --- count: false # Списки Можно хранить данные разного типа .panel1-vec13-user[ ```r *L <- list("A",c(1,2),30) *L ``` ] .panel2-vec13-user[ ``` [[1]] [1] "A" [[2]] [1] 1 2 [[3]] [1] 30 ``` ] --- count: false # Списки Можно хранить данные разного типа .panel1-vec13-user[ ```r L <- list("A",c(1,2),30) L *print('L1') *L1 <- list(L,40) *L1 ``` ] .panel2-vec13-user[ ``` [[1]] [1] "A" [[2]] [1] 1 2 [[3]] [1] 30 ``` ``` [1] "L1" ``` ``` [[1]] [[1]][[1]] [1] "A" [[1]][[2]] [1] 1 2 [[1]][[3]] [1] 30 [[2]] [1] 40 ``` ] <style> .panel1-vec13-user { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-vec13-user { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-vec13-user { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style>
--- count: false # Списки Добавление нового элемента .panel1-vec14-user[ ```r *L[[4]] <- 'new_element' *L ``` ] .panel2-vec14-user[ ``` [[1]] [1] "A" [[2]] [1] 1 2 [[3]] [1] 30 [[4]] [1] "new_element" ``` ] <style> .panel1-vec14-user { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-vec14-user { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-vec14-user { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false # Списки Срезы .panel1-vec15-user[ ```r *L ``` ] .panel2-vec15-user[ ``` [[1]] [1] "A" [[2]] [1] 1 2 [[3]] [1] 30 [[4]] [1] "new_element" ``` ] --- count: false # Списки Срезы .panel1-vec15-user[ ```r L *L[3] ``` ] .panel2-vec15-user[ ``` [[1]] [1] "A" [[2]] [1] 1 2 [[3]] [1] 30 [[4]] [1] "new_element" ``` ``` [[1]] [1] 30 ``` ] --- count: false # Списки Срезы .panel1-vec15-user[ ```r L L[3] *L[[3]] ``` ] .panel2-vec15-user[ ``` [[1]] [1] "A" [[2]] [1] 1 2 [[3]] [1] 30 [[4]] [1] "new_element" ``` ``` [[1]] [1] 30 ``` ``` [1] 30 ``` ] <style> .panel1-vec15-user { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-vec15-user { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-vec15-user { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false # Вложенность .panel1-vec16-user[ ```r *mtcars$mpg ``` ] .panel2-vec16-user[ ``` [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4 [16] 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7 [31] 15.0 21.4 ``` ] --- count: false # Вложенность .panel1-vec16-user[ ```r mtcars$mpg *dim(mtcars$mpg) ``` ] .panel2-vec16-user[ ``` [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4 [16] 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7 [31] 15.0 21.4 ``` ``` NULL ``` ] --- count: false # Вложенность .panel1-vec16-user[ ```r mtcars$mpg dim(mtcars$mpg) *mtcars$mpg[3] ``` ] .panel2-vec16-user[ ``` [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4 [16] 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7 [31] 15.0 21.4 ``` ``` NULL ``` ``` [1] 22.8 ``` ] --- count: false # Вложенность .panel1-vec16-user[ ```r mtcars$mpg dim(mtcars$mpg) mtcars$mpg[3] *dim(mtcars$mpg[3]) ``` ] .panel2-vec16-user[ ``` [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4 [16] 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7 [31] 15.0 21.4 ``` ``` NULL ``` ``` [1] 22.8 ``` ``` NULL ``` ] --- count: false # Вложенность .panel1-vec16-user[ ```r mtcars$mpg dim(mtcars$mpg) mtcars$mpg[3] dim(mtcars$mpg[3]) *mtcars[3,] ``` ] .panel2-vec16-user[ ``` [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4 [16] 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7 [31] 15.0 21.4 ``` ``` NULL ``` ``` [1] 22.8 ``` ``` NULL ``` ``` mpg cyl disp hp drat wt qsec vs am gear carb Datsun 710 22.8 4 108 93 3.85 2.32 18.61 1 1 4 1 ``` ] --- count: false # Вложенность .panel1-vec16-user[ ```r mtcars$mpg dim(mtcars$mpg) mtcars$mpg[3] dim(mtcars$mpg[3]) mtcars[3,] *dim(mtcars[3,]) ``` ] .panel2-vec16-user[ ``` [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4 [16] 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7 [31] 15.0 21.4 ``` ``` NULL ``` ``` [1] 22.8 ``` ``` NULL ``` ``` mpg cyl disp hp drat wt qsec vs am gear carb Datsun 710 22.8 4 108 93 3.85 2.32 18.61 1 1 4 1 ``` ``` [1] 1 11 ``` ] --- count: false # Вложенность .panel1-vec16-user[ ```r mtcars$mpg dim(mtcars$mpg) mtcars$mpg[3] dim(mtcars$mpg[3]) mtcars[3,] dim(mtcars[3,]) *mtcars[3,][2] ``` ] .panel2-vec16-user[ ``` [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4 [16] 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7 [31] 15.0 21.4 ``` ``` NULL ``` ``` [1] 22.8 ``` ``` NULL ``` ``` mpg cyl disp hp drat wt qsec vs am gear carb Datsun 710 22.8 4 108 93 3.85 2.32 18.61 1 1 4 1 ``` ``` [1] 1 11 ``` ``` cyl Datsun 710 4 ``` ] --- count: false # Вложенность .panel1-vec16-user[ ```r mtcars$mpg dim(mtcars$mpg) mtcars$mpg[3] dim(mtcars$mpg[3]) mtcars[3,] dim(mtcars[3,]) mtcars[3,][2] *dim(mtcars[3,][2]) ``` ] .panel2-vec16-user[ ``` [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4 [16] 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7 [31] 15.0 21.4 ``` ``` NULL ``` ``` [1] 22.8 ``` ``` NULL ``` ``` mpg cyl disp hp drat wt qsec vs am gear carb Datsun 710 22.8 4 108 93 3.85 2.32 18.61 1 1 4 1 ``` ``` [1] 1 11 ``` ``` cyl Datsun 710 4 ``` ``` [1] 1 1 ``` ] --- count: false # Вложенность .panel1-vec16-user[ ```r mtcars$mpg dim(mtcars$mpg) mtcars$mpg[3] dim(mtcars$mpg[3]) mtcars[3,] dim(mtcars[3,]) mtcars[3,][2] dim(mtcars[3,][2]) *mtcars[3,][[2]] ``` ] .panel2-vec16-user[ ``` [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4 [16] 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7 [31] 15.0 21.4 ``` ``` NULL ``` ``` [1] 22.8 ``` ``` NULL ``` ``` mpg cyl disp hp drat wt qsec vs am gear carb Datsun 710 22.8 4 108 93 3.85 2.32 18.61 1 1 4 1 ``` ``` [1] 1 11 ``` ``` cyl Datsun 710 4 ``` ``` [1] 1 1 ``` ``` [1] 4 ``` ] --- count: false # Вложенность .panel1-vec16-user[ ```r mtcars$mpg dim(mtcars$mpg) mtcars$mpg[3] dim(mtcars$mpg[3]) mtcars[3,] dim(mtcars[3,]) mtcars[3,][2] dim(mtcars[3,][2]) mtcars[3,][[2]] *dim(mtcars[3,][[2]]) ``` ] .panel2-vec16-user[ ``` [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4 [16] 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7 [31] 15.0 21.4 ``` ``` NULL ``` ``` [1] 22.8 ``` ``` NULL ``` ``` mpg cyl disp hp drat wt qsec vs am gear carb Datsun 710 22.8 4 108 93 3.85 2.32 18.61 1 1 4 1 ``` ``` [1] 1 11 ``` ``` cyl Datsun 710 4 ``` ``` [1] 1 1 ``` ``` [1] 4 ``` ``` NULL ``` ] <style> .panel1-vec16-user { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-vec16-user { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-vec16-user { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false # Списки .panel1-vec17-user[ ```r *L1 ``` ] .panel2-vec17-user[ ``` [[1]] [[1]][[1]] [1] "A" [[1]][[2]] [1] 1 2 [[1]][[3]] [1] 30 [[2]] [1] 40 ``` ] --- count: false # Списки .panel1-vec17-user[ ```r L1 *L1 [[1]] ``` ] .panel2-vec17-user[ ``` [[1]] [[1]][[1]] [1] "A" [[1]][[2]] [1] 1 2 [[1]][[3]] [1] 30 [[2]] [1] 40 ``` ``` [[1]] [1] "A" [[2]] [1] 1 2 [[3]] [1] 30 ``` ] --- count: false # Списки .panel1-vec17-user[ ```r L1 L1 [[1]] *L1 [[1]] [[2]] ``` ] .panel2-vec17-user[ ``` [[1]] [[1]][[1]] [1] "A" [[1]][[2]] [1] 1 2 [[1]][[3]] [1] 30 [[2]] [1] 40 ``` ``` [[1]] [1] "A" [[2]] [1] 1 2 [[3]] [1] 30 ``` ``` [1] 1 2 ``` ] --- count: false # Списки .panel1-vec17-user[ ```r L1 L1 [[1]] L1 [[1]] [[2]] ``` ] .panel2-vec17-user[ ``` [[1]] [[1]][[1]] [1] "A" [[1]][[2]] [1] 1 2 [[1]][[3]] [1] 30 [[2]] [1] 40 ``` ``` [[1]] [1] "A" [[2]] [1] 1 2 [[3]] [1] 30 ``` ``` [1] 1 2 ``` ] <style> .panel1-vec17-user { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-vec17-user { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-vec17-user { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false # Списки .panel1-vec18-user[ ```r *L <- list(10,20) ``` ] .panel2-vec18-user[ ] --- count: false # Списки .panel1-vec18-user[ ```r L <- list(10,20) *L ``` ] .panel2-vec18-user[ ``` [[1]] [1] 10 [[2]] [1] 20 ``` ] --- count: false # Списки .panel1-vec18-user[ ```r L <- list(10,20) L *L$abc <- 123 ``` ] .panel2-vec18-user[ ``` [[1]] [1] 10 [[2]] [1] 20 ``` ] --- count: false # Списки .panel1-vec18-user[ ```r L <- list(10,20) L L$abc <- 123 *L ``` ] .panel2-vec18-user[ ``` [[1]] [1] 10 [[2]] [1] 20 ``` ``` [[1]] [1] 10 [[2]] [1] 20 $abc [1] 123 ``` ] --- count: false # Списки .panel1-vec18-user[ ```r L <- list(10,20) L L$abc <- 123 L *names(L) ``` ] .panel2-vec18-user[ ``` [[1]] [1] 10 [[2]] [1] 20 ``` ``` [[1]] [1] 10 [[2]] [1] 20 $abc [1] 123 ``` ``` [1] "" "" "abc" ``` ] --- count: false # Списки .panel1-vec18-user[ ```r L <- list(10,20) L L$abc <- 123 L names(L) *L[[3]] ``` ] .panel2-vec18-user[ ``` [[1]] [1] 10 [[2]] [1] 20 ``` ``` [[1]] [1] 10 [[2]] [1] 20 $abc [1] 123 ``` ``` [1] "" "" "abc" ``` ``` [1] 123 ``` ] --- count: false # Списки .panel1-vec18-user[ ```r L <- list(10,20) L L$abc <- 123 L names(L) L[[3]] *L$abc ``` ] .panel2-vec18-user[ ``` [[1]] [1] 10 [[2]] [1] 20 ``` ``` [[1]] [1] 10 [[2]] [1] 20 $abc [1] 123 ``` ``` [1] "" "" "abc" ``` ``` [1] 123 ``` ``` [1] 123 ``` ] --- count: false # Списки .panel1-vec18-user[ ```r L <- list(10,20) L L$abc <- 123 L names(L) L[[3]] L$abc *L[['abc']] ``` ] .panel2-vec18-user[ ``` [[1]] [1] 10 [[2]] [1] 20 ``` ``` [[1]] [1] 10 [[2]] [1] 20 $abc [1] 123 ``` ``` [1] "" "" "abc" ``` ``` [1] 123 ``` ``` [1] 123 ``` ``` [1] 123 ``` ] <style> .panel1-vec18-user { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-vec18-user { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-vec18-user { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- # Циклы Циклы в R медленные! --- # for ```r for(year in c(2010:2015)){ print(paste('The year is', year)) } ``` ``` [1] "The year is 2010" [1] "The year is 2011" [1] "The year is 2012" [1] "The year is 2013" [1] "The year is 2014" [1] "The year is 2015" ``` --- # for ```r mt <- mtcars head(mt,2) ``` ``` mpg cyl disp hp drat wt qsec vs am gear carb Mazda RX4 21 6 160 110 3.9 2.620 16.46 0 1 4 4 Mazda RX4 Wag 21 6 160 110 3.9 2.875 17.02 0 1 4 4 ``` ```r for(i in 1:nrow(mt)){ mt$new[i] <- i^2 } head(mt,4) ``` ``` mpg cyl disp hp drat wt qsec vs am gear carb new Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 1 Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 4 Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 9 Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 16 ``` --- # Деление
```r 5/2 ``` ``` [1] 2.5 ``` ```r 5%%2 ``` ``` [1] 1 ``` --- # for + if ```r mt <- mtcars for(i in 1:nrow(mt)){ if((i%%2) == 0){ mt$new[i] = i^2 mt$type[i] = 'even' } else{ mt$new[i] = i^3 mt$type[i] = 'odd' } } mt[1:4,(ncol(mt)-5):ncol(mt)] ``` ``` vs am gear carb new type Mazda RX4 0 1 4 4 1 odd Mazda RX4 Wag 0 1 4 4 4 even Datsun 710 1 1 4 1 27 odd Hornet 4 Drive 1 0 3 1 16 even ``` --- # ifelse ```r v = c(14,7,6,9,2) ifelse(v %% 2 == 1,"odd","even") ``` ``` [1] "even" "odd" "even" "odd" "even" ``` --- # for + if + break ```r x <- 1:5 for(val in x){ if (val == 3){ break } print(val) } ``` ``` [1] 1 [1] 2 ``` --- # for + if + next ```r x <- 1:5 for(val in x){ if (val == 3){ next } print(val) } ``` ``` [1] 1 [1] 2 [1] 4 [1] 5 ``` --- # while ```r i <- 1 while (i < 6){ print(i) i = i+1 } ``` ``` [1] 1 [1] 2 [1] 3 [1] 4 [1] 5 ``` --- # Случайная матрица ```r set.seed(123) mt <- matrix(sample(1:5,10000,replace=T),ncol=10) dim(mt) ``` ``` [1] 1000 10 ``` ```r mt[1:5,1:5] ``` ``` [,1] [,2] [,3] [,4] [,5] [1,] 3 4 5 4 1 [2,] 3 4 4 1 2 [3,] 2 2 3 2 3 [4,] 2 4 1 4 5 [5,] 3 5 2 4 2 ``` --- # Случайная матрица ```r colnames(mt) <- paste("D",1:ncol(mt),sep='_') rownames(mt) <- paste("Stud",1:nrow(mt),sep='.') head(mt) ``` ``` D_1 D_2 D_3 D_4 D_5 D_6 D_7 D_8 D_9 D_10 Stud.1 3 4 5 4 1 1 5 1 5 1 Stud.2 3 4 4 1 2 3 5 1 3 3 Stud.3 2 2 3 2 3 3 5 5 1 3 Stud.4 2 4 1 4 5 2 3 5 5 2 Stud.5 3 5 2 4 2 3 5 1 3 1 Stud.6 5 4 5 5 1 1 5 3 3 4 ``` --- # Создать вектор из средних по столбцам mt ## Давайте сначала создадим вектор нужной длины, заполним NA ```r x <- rep(NA,ncol(mt)) length(x) ``` ``` [1] 10 ``` ```r x ``` ``` [1] NA NA NA NA NA NA NA NA NA NA ``` --- # Создать вектор из средних по столбцам mt ## Теперь заполним созданный вектор средними ```r for(i in 1:ncol(mt)){ x[i] <- mean(mt[,i]) } x ``` ``` [1] 2.985 2.987 2.966 2.954 2.974 3.003 2.960 2.938 2.989 2.962 ``` --- # apply ```r x.2 <- apply(mt,2,mean) x ``` ``` [1] 2.985 2.987 2.966 2.954 2.974 3.003 2.960 2.938 2.989 2.962 ``` ```r x.2 ``` ``` D_1 D_2 D_3 D_4 D_5 D_6 D_7 D_8 D_9 D_10 2.985 2.987 2.966 2.954 2.974 3.003 2.960 2.938 2.989 2.962 ``` ```r a <- apply(mt,1,sum) a[1:6] ``` ``` Stud.1 Stud.2 Stud.3 Stud.4 Stud.5 Stud.6 30 29 29 33 29 36 ``` --- # lapply ## Возвращает список ```r x <- list(a=1,b=1:3,d=10:100) lapply(x, FUN = length) ``` ``` $a [1] 1 $b [1] 3 $d [1] 91 ``` --- # sapply ## Возвращает вектор (если не получается - список) ```r x <- list(a=1,b=1:3,d=10:100) sapply(x, FUN = length) ``` ``` a b d 1 3 91 ```