Vít Gabrhel
vit.gabrhel@mail.muni.cz
FSS MU,
25. 9. 2017
0.
1. Vector
2. Matrix
3. Factor
4. Data Frame
Vector je jednoduchý datový objekt o různé délce obsahující hodnoty
Počet cursing words dle filmů
Co je co aneb pojmenování vektorů
Vektorová aritmetika
Sčítání vektorů
Součet hodnot ve vektoru
Výběr hodnot(y) z vektoru
Logické operátory
< for less than
> for greater than
<= for less than or equal to
>= for greater than or equal to
== for equal to each other
!= not equal to each other
Zaznívalo ve filmech více slovo "Hell" nebo "Goddamn"?
Porovnání hodnot(y) mezi vektory
Ve kterých filmech padlo více cursing words, než byl jejich průměrný počet za osm filmů?
Filmy = c("Kill Bill 1", "Reservoir Dogs", "Inglorious Basterds", "Pulp Fiction")
class(Filmy)
Nominální kategorie
Factor_Filmy = as.factor(Filmy)
class(Factor_Filmy)
levels(Factor_Filmy) <- c("Reservoir Dogs", "Pulp Fiction", "Kill Bill 1", "Inglorious Basterds")
Ordinalizace
Factor_Filmy <- factor(Filmy, order = TRUE, levels = c("Reservoir Dogs", "Pulp Fiction", "Kill Bill 1", "Inglorious Basterds"))
In R, a matrix is
You can construct a matrix in R with the matrix() function. Consider the following example:
The first argument is the collection of elements that R will arrange into the rows and columns of the matrix. Here, we use 1:9 which is a shortcut for c(1, 2, 3, 4, 5, 6, 7, 8, 9).
The argument byrow indicates that the matrix is filled by the rows. If we want the matrix to be filled by the columns, we just place byrow = FALSE.
The third argument nrow indicates that the matrix should have three rows.
O cursing words v Tarantinových filmech už něco víme. Co ale počet mrtvých?
Budeme se věnovat Pulp Fiction, Inglorious Basterds a Django Unchained spolu s počtem zesnulých postav. Přidáme k tomu známý počet cursing words v příslušných filmech:
Filmy <- matrix(c(Pulp_Fiction, Inglorious_Basterds, Django_Unchained), nrow = 3, byrow = TRUE)
View(Filmy)
Pojmenování řádků/sloupců
rownames(Filmy) <- c("Pulp_Fiction", "Inglorious_Basterds", "Django_Unchained")
colnames(Filmy) <- c("Deaths", "Words")
View(Filmy)
Death_Curse = c(7, 48, 47, 469, 58, 262)
Death_Curse_Matrix = matrix(Death_Curse, nrow = 3, byrow = FALSE,
dimnames = list(c("Pulp_Fiction", "Inglorious_Basterds", "Django_Unchained")))
colnames(Death_Curse_Matrix) <- c("Deaths", "Curses")
colSums(Death_Curse_Matrix)
Jak do matice přidat sloupec / řádek?
Skrze příkaz cbind() / rbind()
Filmy si rozdělíme z hlediska období tvorby (90s, 00s a 10s) s kódy "0", "1" a "2":
Period = c(0, 1, 2)
Period_Matrix = matrix(Period)
rownames(Period_Matrix) = c("90s", "00s", "10s")
colnames(Period_Matrix) <- c("Period")
Death_Curse_Period = cbind(Death_Curse_Matrix, Period_Matrix)
Death_Curse_Period
Jak příkazem zjistit aktivní objekty?
ls()
Jak vybrat konkrétní prvky z matice?
Similar to vectors, you can use the square brackets [ ] to select one or multiple elements from a matrix.
Jaký byl průměrný počet mrtvých ve sledovaných filmech?
Mean_Dead = Death_Curse_Period[,1]
mean(Mean_Dead)
Jaký je Tarantino index (tj. počet mrtvých na počet nadávek) pro Inglorious Basterds?
Dead_Curse = data.frame(Death_Curse_Period[2,1:2])
Dead_Curse[2,1]/Dead_Curse[1,1]
Data Frame je matice tak, jak ji chápeme při analýze dat
V čem se v R "Data Frame" liší od "Matrix"?
Vyvolání Data Frame z R
data()
data(USArrests)
View(USArrests)
??USArrests
Jak se zorientovat v Data Frame?
Tvorba vlastní Data Frame
data.frame()
# Planety - definování vektorů
# Planety
# Struktura dat
Výběr prvků
Subsoubory
Seřazování
Cornelissen, J. (n.d.) Introduction to R. Dostupné online na:
https://www.datacamp.com/courses/free-introduction-to-r