library(MASS)
View(painters)
<Adjust Rows>
When you try to adjust Rows ,that is trying to extract some rows that satisfies some conditions, you must handle left side of the comma in the bracket. Such as painters[(Conditions you want to see in rows) , ]. In contrast, you will see what happens when you want to handle the column of data frames. In summary, left side of the bracket is for rows and the right side is for columns.
data frame[conditions for rows, conditions for columns].
>painters[c("Bassano","Giorgione"),]
>painters[c(3,5),]
>painters[painters$Colour>=17,] |
> ind = painters$Colour >= 17 > painters[ind,] |
Composition Drawing Colour Expression School Bassano 6 8 17 0 D Giorgione 8 9 18 4 D Pordenone 8 14 17 5 D Titian 12 15 18 6 D Rembrandt 15 6 17 12 G Rubens 18 13 17 17 G Van Dyck 15 10 17 13 G |
Composition Drawing Colour Expression School Bassano 6 8 17 0 D Giorgione 8 9 18 4 D Pordenone 8 14 17 5 D Titian 12 15 18 6 D Rembrandt 15 6 17 12 G Rubens 18 13 17 17 G Van Dyck 15 10 17 13 G |
<Using 'which' function>
> ind = which(painters$Colour >= 17)
> ind
[1] 23 25 29 31 47 48 50
> painters[ind,]
Composition Drawing Colour Expression School
Bassano 6 8 17 0 D
Giorgione 8 9 18 4 D
Pordenone 8 14 17 5 D
Titian 12 15 18 6 D
Rembrandt 15 6 17 12 G
Rubens 18 13 17 17 G
Van Dyck 15 10 17 13 G
<Using 'is.element' function>
> ind = is.element(painters$School, c("A", "D"))
> painters[ind,]
Composition Drawing Colour Expression School
Da Udine 10 8 16 3 A
Da Vinci 15 16 4 14 A
Del Piombo 8 13 16 7 A
Del Sarto 12 16 9 8 A
Fr. Penni 0 15 8 0 A
Guilio Romano 15 16 4 14 A
Michelangelo 8 17 4 8 A
Perino del Vaga 15 16 7 6 A
Perugino 4 12 10 4 A
Raphael 17 18 12 18 A
Bassano 6 8 17 0 D
Bellini 4 6 14 0 D
Giorgione 8 9 18 4 D
Murillo 6 8 15 4 D
Palma Giovane 12 9 14 6 D
Palma Vecchio 5 6 16 0 D
Pordenone 8 14 17 5 D
Tintoretto 15 14 16 4 D
Titian 12 15 18 6 D
Veronese 15 10 16 3 D
<Adjust Columns>
You will see what happens when you want to handle the column of data frames. In summary, left side of the bracket is for rows and the right side is for columns.
data frame[conditions for rows, conditions for columns].
> painters[ , c("School", "Colour")]
School Colour
Da Udine A 16
Da Vinci A 4
Del Piombo A 16
Del Sarto A 9
Fr. Penni A 8
Guilio Romano A 4
Michelangelo A 4
Perino del Vaga A 7
Perugino A 10
Raphael A 12
F. Zucarro B 8
Fr. Salviata B 8
Parmigiano B 6
Primaticcio B 7
T. Zucarro B 10
Volterra B 5
Barocci C 6
Cortona C 12
Josepin C 6
L. Jordaens C 9
Testa C 0
Vanius C 12
Bassano D 17
Bellini D 14
Giorgione D 18
Murillo D 15
Palma Giovane D 14
Palma Vecchio D 16
Pordenone D 17
Tintoretto D 16
Titian D 18
Veronese D 16
Albani E 10
Caravaggio E 16
Corregio E 15
Domenichino E 9
Guercino E 10
Lanfranco E 10
The Carraci E 13
Durer F 10
Holbein F 16
Pourbus F 6
Van Leyden F 6
Diepenbeck G 14
J. Jordaens G 16
Otho Venius G 10
Rembrandt G 17
Rubens G 17
Teniers G 13
Van Dyck G 17
Bourdon H 8
Le Brun H 8
Le Suer H 4
Poussin H 6
> d2 = painters[,c(5, 3)]
> d2
School Colour
Da Udine A 16
Da Vinci A 4
Del Piombo A 16
Del Sarto A 9
Fr. Penni A 8
Guilio Romano A 4
Michelangelo A 4
Perino del Vaga A 7
Perugino A 10
Raphael A 12
F. Zucarro B 8
Fr. Salviata B 8
Parmigiano B 6
Primaticcio B 7
T. Zucarro B 10
Volterra B 5
Barocci C 6
Cortona C 12
Josepin C 6
L. Jordaens C 9
Testa C 0
Vanius C 12
Bassano D 17
Bellini D 14
Giorgione D 18
Murillo D 15
Palma Giovane D 14
Palma Vecchio D 16
Pordenone D 17
Tintoretto D 16
Titian D 18
Veronese D 16
Albani E 10
Caravaggio E 16
Corregio E 15
Domenichino E 9
Guercino E 10
Lanfranco E 10
The Carraci E 13
Durer F 10
Holbein F 16
Pourbus F 6
Van Leyden F 6
Diepenbeck G 14
J. Jordaens G 16
Otho Venius G 10
Rembrandt G 17
Rubens G 17
Teniers G 13
Van Dyck G 17
Bourdon H 8
Le Brun H 8
Le Suer H 4
Poussin H 6
<Extract Columns and Rows>
# Select "Bassano" and "Giorgione" rows and "Composition" and "Drawing" columns from data frame.
> painters[c("Bassano","Giorgione"), c("Composition","Drawing")]
Composition Drawing
Bassano 6 8
Giorgione 8 9
>painters[painters$School=="A",c(1,2)]
Composition Drawing
Da Udine 10 8
Da Vinci 15 16
Del Piombo 8 13
Del Sarto 12 16
Fr. Penni 0 15
Guilio Romano 15 16
Michelangelo 8 17
Perino del Vaga 15 16
Perugino 4 12
Raphael 17 18
<Using Subset function>
# Make a subset by setting a condition ( Colour>=15 ). Also you can select 1:3.
> subset(painters, subset = Colour >= 15, select = 1:3)
Composition Drawing Colour
Da Udine 10 8 16
Del Piombo 8 13 16
Bassano 6 8 17
Giorgione 8 9 18
Murillo 6 8 15
Palma Vecchio 5 6 16
Pordenone 8 14 17
Tintoretto 15 14 16
Titian 12 15 18
Veronese 15 10 16
Caravaggio 6 6 16
Corregio 13 13 15
Holbein 9 10 16
J. Jordaens 10 8 16
Rembrandt 15 6 17
Rubens 18 13 17
Van Dyck 15 10 17
>subset(painters, subset = Colour >= 15, select = c(1, 2))
Composition Drawing
Da Udine 10 8
Del Piombo 8 13
Bassano 6 8
Giorgione 8 9
Murillo 6 8
Palma Vecchio 5 6
Pordenone 8 14
Tintoretto 15 14
Titian 12 15
Veronese 15 10
Caravaggio 6 6
Corregio 13 13
Holbein 9 10
J. Jordaens 10 8
Rembrandt 15 6
Rubens 18 13
Van Dyck 15 10
> subset(painters, subset = Colour >= 15, select = c(Composition, Drawing))
Composition Drawing
Da Udine 10 8
Del Piombo 8 13
Bassano 6 8
Giorgione 8 9
Murillo 6 8
Palma Vecchio 5 6
Pordenone 8 14
Tintoretto 15 14
Titian 12 15
Veronese 15 10
Caravaggio 6 6
Corregio 13 13
Holbein 9 10
J. Jordaens 10 8
Rembrandt 15 6
Rubens 18 13
Van Dyck 15 10
>subset(painters, subset = Colour >= 15, select = -c(Composition, Drawing))
Colour Expression School
Da Udine 16 3 A
Del Piombo 16 7 A
Bassano 17 0 D
Giorgione 18 4 D
Murillo 15 4 D
Palma Vecchio 16 0 D
Pordenone 17 5 D
Tintoretto 16 4 D
Titian 18 6 D
Veronese 16 3 D
Caravaggio 16 0 E
Corregio 15 12 E
Holbein 16 13 F
J. Jordaens 16 6 G
Rembrandt 17 12 G
Rubens 17 17 G
Van Dyck 17 13 G
'R' 카테고리의 다른 글
(R) Combining Data frame, matrix and vectors using rbind, cbind / Merging data frames using merge() (0) | 2020.11.07 |
---|---|
(R) Make a new variable (ex,quantile) (0) | 2020.11.05 |
(R)Wilkinson dot plot (+boxplot) (0) | 2020.10.29 |
(R) Two sample t-test (Student's t, Welch's t ) (0) | 2020.10.27 |
(R) Permutation Test - Non parametric method (0) | 2020.10.24 |