R version 2.13.0 (2011-04-13) Copyright (C) 2011 The R Foundation for Statistical Computing ISBN 3-900051-07-0 Platform: i386-pc-mingw32/i386 (32-bit) R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > source("cv2.R") MATICE, POLE, TABULKY Na dalsi ukol se dostanete klavesou c nebo ENTER. 1. Vytvorte vektor "v" rozsahu 20 s krokem 2 a konecnou hodnotou 20. Pak jej seskladejte po sloupcich do matice "A" o 4 radcich a 5 sloupcich. Called from: eval.with.vis(expr, envir, enclos) Browse[1]> #prikaz Browse[1]> v [1] -18 -16 -14 -12 -10 -8 -6 -4 -2 0 2 4 6 8 10 12 14 16 18 [20] 20 Browse[1]> #prikaz Browse[1]> A [,1] [,2] [,3] [,4] [,5] [1,] -18 -10 -2 6 14 [2,] -16 -8 0 8 16 [3,] -14 -6 2 10 18 [4,] -12 -4 4 12 20 Browse[1]> 2. Provedte stejny ukol s argumentem byrow=TRUE, pridejte navic nazvy radku a sloupcu. Called from: eval.with.vis(expr, envir, enclos) Browse[1]> #prikaz Browse[1]> A sl1 sl2 sl3 sl4 sl5 r1 -18 -16 -14 -12 -10 r2 -8 -6 -4 -2 0 r3 2 4 6 8 10 r4 12 14 16 18 20 Browse[1]> 3. Vektor "v" seskladejte do pole "B" o dimenzich 2x5x2, dimenze opet pojmenujte. Called from: eval.with.vis(expr, envir, enclos) Browse[1]> #prikaz Browse[1]> B , , dim1 sl1 sl2 sl3 sl4 sl5 r1 -18 -14 -10 -6 -2 r2 -16 -12 -8 -4 0 , , dim2 sl1 sl2 sl3 sl4 sl5 r1 2 6 10 14 18 r2 4 8 12 16 20 Browse[1]> 4. Vytvorte vektor "a"=[1, 2, 3] a matice "m" rozmeru 2x3 se vsemi prvky rovnymi 5 a "n" rozmeru 2x4 s prvky tvoricimi posloupnost 8, 6, 4,... . Pomoci funkci "rbind" a "cbind" zkousejte tyto objekty skladat do matic. Jaka musi platit pravidla, aby se prikazy bezchybne provedly? Called from: eval.with.vis(expr, envir, enclos) Browse[1]> # prikaz Browse[1]> a [1] 1 2 3 Browse[1]> # prikaz Browse[1]> m [,1] [,2] [,3] [1,] 5 5 5 [2,] 5 5 5 Browse[1]> # prikaz Browse[1]> n [,1] [,2] [,3] [,4] [1,] 8 4 0 -4 [2,] 6 2 -2 -6 Browse[1]> # prikaz ### slouceni po sloupcich [,1] [,2] [,3] [,4] [,5] [,6] [,7] [1,] 5 5 5 8 4 0 -4 [2,] 5 5 5 6 2 -2 -6 Browse[1]> # prikaz ### slouceni po radcich [,1] [,2] [,3] a 1 2 3 5 5 5 5 5 5 Browse[1]> 5. Do matice "C" vlozte matici "A" a na jeji liche radky a sude sloupce umistete nuly. Called from: eval.with.vis(expr, envir, enclos) Browse[1]> #prikaz Browse[1]> C sl1 sl2 sl3 sl4 sl5 r1 -18 -16 -14 -12 -10 r2 -8 -6 -4 -2 0 r3 2 4 6 8 10 r4 12 14 16 18 20 Browse[1]> #prikaz Browse[1]> C sl1 sl2 sl3 sl4 sl5 r1 -18 0 -14 0 -10 r2 -8 -6 -4 -2 0 r3 2 0 6 0 10 r4 12 14 16 18 20 Browse[1]> 6. Na stejne pozice jako v predeslem prikladu umistete textovy retezec "text". Vypiste nove vzniklou matici a vysledek zduvodnete. Called from: eval.with.vis(expr, envir, enclos) Browse[1]> #prikaz Browse[1]> C sl1 sl2 sl3 sl4 sl5 r1 "-18" "text" "-14" "text" "-10" r2 "-8" "-6" "-4" "-2" "0" r3 "2" "text" "6" "text" "10" r4 "12" "14" "16" "18" "20" Browse[1]> 7. Vypiste 4. sloupec matice "A" ve forme radkoveho i sloupcoveho vektoru. Called from: eval.with.vis(expr, envir, enclos) Browse[1]> #prikaz ### radkovy vektor r1 r2 r3 r4 -12 -2 8 18 Browse[1]> #prikaz ### sloupcovy vektor sl4 r1 -12 r2 -2 r3 8 r4 18 Browse[1]> 8. Odstrante z matice "A" posledni radek a prvni sloupec. Called from: eval.with.vis(expr, envir, enclos) Browse[1]> #prikaz sl2 sl3 sl4 sl5 r1 -16 -14 -12 -10 r2 -6 -4 -2 0 r3 4 6 8 10 Browse[1]> 9. Zjistete rozmery matice "A" i pole "B", transponujte matici "A". Called from: eval.with.vis(expr, envir, enclos) Browse[1]> #prikaz ### rozmer A [1] 4 5 Browse[1]> #prikaz ### rozmer B [1] 2 5 2 Browse[1]> #prikaz ### transpozice r1 r2 r3 r4 sl1 -18 -8 2 12 sl2 -16 -6 4 14 sl3 -14 -4 6 16 sl4 -12 -2 8 18 sl5 -10 0 10 20 Browse[1]> 10. Vypiste diagonalu matice "A". Dale vytvorte matici "D", ktera bude mit stejne diagonalni prvky jako matice "A" a mimo diagonalu same nuly. Called from: eval.with.vis(expr, envir, enclos) Browse[1]> #prikaz ### diagonala [1] -18 -6 6 18 Browse[1]> #prikaz ### diagonalni matice Browse[1]> D [,1] [,2] [,3] [,4] [1,] -18 0 0 0 [2,] 0 -6 0 0 [3,] 0 0 6 0 [4,] 0 0 0 18 Browse[1]> 11. Vytvorte jednotkovou matici o peti radcich. Called from: eval.with.vis(expr, envir, enclos) Browse[1]> #prikaz [,1] [,2] [,3] [,4] [,5] [1,] 1 0 0 0 0 [2,] 0 1 0 0 0 [3,] 0 0 1 0 0 [4,] 0 0 0 1 0 [5,] 0 0 0 0 1 Browse[1]> 12. Vytvorte ctvercovou matici "E" radu 3 radu s prvky 2, 0, 0, 2, 2, 1, 1, 1, 2 seskladanymi po radcich. Dale spocitejte determinant, vlastni cisla, vlastni vektory, normu (nutno nainstalovat balik "Matrix") a stopu matice "E". Called from: eval.with.vis(expr, envir, enclos) Browse[1]> #prikaz Browse[1]> E [,1] [,2] [,3] [1,] 2 0 0 [2,] 2 2 1 [3,] 1 1 2 Browse[1]> #prikaz ### determinant [1] 6 Browse[1]> #prikaz ### vlastni cisla [1] 3 2 1 Browse[1]> #prikaz ### vlastni vektory [,1] [,2] [,3] [1,] 0.0000000 0.4082483 0.0000000 [2,] 0.7071068 -0.4082483 0.7071068 [3,] 0.7071068 -0.8164966 -0.7071068 Browse[1]> install.packages("Matrix") Browse[1]> library(Matrix) Browse[1]> #prikaz ### norma [1] 5 Browse[1]> #prikaz ### stopa [1] 6 Browse[1]> 13. Vektor hodnot 2, 1, -2, -1, 2, 1, 2, 3, 1 seskladejte po radcich do matice "F" o 3 sloupcich a reste system linearnich rovnic s pravou stranou "b"=(7, -1, 0). Called from: eval.with.vis(expr, envir, enclos) Browse[1]> #prikaz Browse[1]> F [,1] [,2] [,3] [1,] 2 1 -2 [2,] -1 2 1 [3,] 2 3 1 Browse[1]> #prikaz Browse[1]> b [,1] [1,] 7 [2,] -1 [3,] 0 Browse[1]> #prikaz ### x - reseni soustavy x Browse[1]> x [,1] [1,] 0 [2,] 1 [3,] -3 Browse[1]> 14. Vytvorte datovou tabulku "studenti" se sloupci "vyska", "pohlavi", "bydliste": vektor "vyska" obsahuje hodnoty 178, 173, 158, 190, 167, "pohlavi" hodnoty "muz", "zena", "zena", "muz", "muz", "bydliste" hodnoty "Brno", "Praha", "Brno", "Plzen", "Ostrava" a nazvy pripadu "Petr", "Tereza", "Karolina", "Filip", "Jiri". Called from: eval.with.vis(expr, envir, enclos) Browse[1]> #prikaz Browse[1]> studenti vyska pohlavi bydliste Petr 178 muz Brno Tereza 173 zena Praha Karolina 158 zena Brno Filip 190 muz Plzen Jiri 167 muz Ostrava Browse[1]> 15. Zjistete rozmery a nazvy promennych tabulky "studenti". Pomoci funkce "contents" vypiste detailni vysledky objektu "studenti". Pro funkci "contents" je treba nainstalovat balik "Hmisc". Called from: eval.with.vis(expr, envir, enclos) Browse[1]> #prikaz ### rozmery [1] 5 3 Browse[1]> #prikaz ### nazvy promennych [1] "vyska" "pohlavi" "bydliste" Browse[1]> install.packages("Hmisc") Browse[1]> library(Hmisc) Browse[1]> #prikaz ### detailni vysledky Data frame:studenti 5 observations and 3 variables Maximum # NAs:0 Levels Storage vyska double pohlavi 2 integer bydliste 4 integer +--------+------------------------+ |Variable|Levels | +--------+------------------------+ |pohlavi |muz,zena | +--------+------------------------+ |bydliste|Brno,Ostrava,Plzen,Praha| +--------+------------------------+ Browse[1]> 16. Vytvorte seznam "s" o trech slozkach: textovy vektor "barva" se slozkami "cervena", "modra", "zelena", "zluta", numericky vektor "pocet" delky 5 tvoreny nahodnymi celymi cisly z intervalu [-3,7], "pole" rozmeru 3x2x2 se vsemi hodnotami rovnymi 3. Called from: eval.with.vis(expr, envir, enclos) Browse[1]> #prikaz Browse[1]> s $barva [1] "cervena" "modra" "zelena" "zluta" $pocet [1] 5 4 2 0 -2 $pole , , 1 [,1] [,2] [1,] 3 3 [2,] 3 3 [3,] 3 3 , , 2 [,1] [,2] [1,] 3 3 [2,] 3 3 [3,] 3 3 Browse[1]> 17. Zjistete pocty a nazvy slozek seznamu "s". Called from: eval.with.vis(expr, envir, enclos) Browse[1]> #prikaz ### pocet slozek [1] 3 Browse[1]> #prikaz ### nazvy slozek [1] "barva" "pocet" "pole" Browse[1]> 18. Z datove tabulky "studenti" zjistete vysku a bydliste Karoliny a Filipa. Called from: eval.with.vis(expr, envir, enclos) Browse[1]> #prikaz vyska bydliste Karolina 158 Brno Filip 190 Plzen Browse[1]> 19. Ze seznamu "s" vypiste treti slozku. Called from: eval.with.vis(expr, envir, enclos) Browse[1]> #prikaz $pole , , 1 [,1] [,2] [1,] 3 3 [2,] 3 3 [3,] 3 3 , , 2 [,1] [,2] [1,] 3 3 [2,] 3 3 [3,] 3 3 Browse[1]> 20. K datove tabulce "studenti" pridejte dalsi sloupec "vzdelani" s hodnotami "SS", "VS", "VS", "VS", "SS". Vyzkousejte ruzne zpusoby. Called from: eval.with.vis(expr, envir, enclos) Browse[1]> #prikaz Browse[1]> studenti vyska pohlavi bydliste vzdelani Petr 178 muz Brno SS Tereza 173 zena Praha VS Karolina 158 zena Brno VS Filip 190 muz Plzen VS Jiri 167 muz Ostrava SS Browse[1]> 21. Seradte datovou tabulku "studenti" podle pohlavi, prip. podle vysky. Called from: eval.with.vis(expr, envir, enclos) Browse[1]> #prikaz vyska pohlavi bydliste vzdelani Jiri 167 muz Ostrava SS Petr 178 muz Brno SS Filip 190 muz Plzen VS Karolina 158 zena Brno VS Tereza 173 zena Praha VS Browse[1]> >