# the R book por Michael Crawley
# capitulo 4
setwd("C:\\Users\\Luis\\Documents\\therbook")
worm <- read.table("C:\\Users\\Luis\\Documents\\therbook\\worms.txt",
                   header = TRUE,sep = "")
worm
##           Field.Name Area Slope Vegetation Soil.pH  Damp Worm.density
## 1        Nashs.Field  3.6    11  Grassland     4.1 FALSE            4
## 2     Silwood.Bottom  5.1     2     Arable     5.2 FALSE            7
## 3      Nursery.Field  2.8     3  Grassland     4.3 FALSE            2
## 4        Rush.Meadow  2.4     5     Meadow     4.9  TRUE            5
## 5    Gunness.Thicket  3.8     0      Scrub     4.2 FALSE            6
## 6           Oak.Mead  3.1     2  Grassland     3.9 FALSE            2
## 7       Church.Field  3.5     3  Grassland     4.2 FALSE            3
## 8            Ashurst  2.1     0     Arable     4.8 FALSE            4
## 9        The.Orchard  1.9     0    Orchard     5.7 FALSE            9
## 10     Rookery.Slope  1.5     4  Grassland     5.0  TRUE            7
## 11       Garden.Wood  2.9    10      Scrub     5.2 FALSE            8
## 12      North.Gravel  3.3     1  Grassland     4.1 FALSE            1
## 13      South.Gravel  3.7     2  Grassland     4.0 FALSE            2
## 14 Observatory.Ridge  1.8     6  Grassland     3.8 FALSE            0
## 15        Pond.Field  4.1     0     Meadow     5.0  TRUE            6
## 16      Water.Meadow  3.9     0     Meadow     4.9  TRUE            8
## 17         Cheapside  2.2     8      Scrub     4.7  TRUE            4
## 18        Pound.Hill  4.4     2     Arable     4.5 FALSE            5
## 19        Gravel.Pit  2.9     1  Grassland     3.5 FALSE            1
## 20         Farm.Wood  0.8    10      Scrub     5.1  TRUE            3
#by(worm,worm$Vegetation,FUN=mean)# no funciona
by(worm,worm$Vegetation,function(x) lm(worm$Worm.density~worm$Soil.pH,data=x))
## worm$Vegetation: Arable
## 
## Call:
## lm(formula = worm$Worm.density ~ worm$Soil.pH, data = x)
## 
## Coefficients:
##  (Intercept)  worm$Soil.pH  
##      -12.771         3.759  
## 
## -------------------------------------------------------- 
## worm$Vegetation: Grassland
## 
## Call:
## lm(formula = worm$Worm.density ~ worm$Soil.pH, data = x)
## 
## Coefficients:
##  (Intercept)  worm$Soil.pH  
##      -12.771         3.759  
## 
## -------------------------------------------------------- 
## worm$Vegetation: Meadow
## 
## Call:
## lm(formula = worm$Worm.density ~ worm$Soil.pH, data = x)
## 
## Coefficients:
##  (Intercept)  worm$Soil.pH  
##      -12.771         3.759  
## 
## -------------------------------------------------------- 
## worm$Vegetation: Orchard
## 
## Call:
## lm(formula = worm$Worm.density ~ worm$Soil.pH, data = x)
## 
## Coefficients:
##  (Intercept)  worm$Soil.pH  
##      -12.771         3.759  
## 
## -------------------------------------------------------- 
## worm$Vegetation: Scrub
## 
## Call:
## lm(formula = worm$Worm.density ~ worm$Soil.pH, data = x)
## 
## Coefficients:
##  (Intercept)  worm$Soil.pH  
##      -12.771         3.759
worm[3,5]# extrae el valor de la fila 3 y la columna 5(Soil.pH)
## [1] 4.3
worm[10:13,7]#extrae los valores del lugar 10 al 13 en la 7ma. variable
## [1] 7 8 1 2
#Suponga que deseamos el Area y Slope (columnas 2 y 3) de la filas 1 to 5:
worm[1:5,2:3]
##   Area Slope
## 1  3.6    11
## 2  5.1     2
## 3  2.8     3
## 4  2.4     5
## 5  3.8     0
# Para seleccionar todos los valores en una fila
# la sintaxis es 'numbero coma blanco'. De igual forma, 
# para seleccionar todos los valores en una columna la sintaxis 'blanco coma
# numero'. Asi, para seleccionar todas las columnas en la fila 3:
worm[3,]
##      Field.Name Area Slope Vegetation Soil.pH  Damp Worm.density
## 3 Nursery.Field  2.8     3  Grassland     4.3 FALSE            2
class(worm[3,])
## [1] "data.frame"
# para seleccionar todas las filas en la columna 3:
worm[,3]
##  [1] 11  2  3  5  0  2  3  0  0  4 10  1  2  6  0  0  8  2  1 10
class(worm[,3])
## [1] "integer"
# Podemos crear conjuntos de filas y columnas.Por ejemplo,
# para extraer todas las filas de  Field.Name y
# Soil.pH (columns 1 and 5) usamos la funcion concatenate,
# c, para hacer un vector de la requerida columna
# numeros c(1,5):
worm[,c(1,5)]
##           Field.Name Soil.pH
## 1        Nashs.Field     4.1
## 2     Silwood.Bottom     5.2
## 3      Nursery.Field     4.3
## 4        Rush.Meadow     4.9
## 5    Gunness.Thicket     4.2
## 6           Oak.Mead     3.9
## 7       Church.Field     4.2
## 8            Ashurst     4.8
## 9        The.Orchard     5.7
## 10     Rookery.Slope     5.0
## 11       Garden.Wood     5.2
## 12      North.Gravel     4.1
## 13      South.Gravel     4.0
## 14 Observatory.Ridge     3.8
## 15        Pond.Field     5.0
## 16      Water.Meadow     4.9
## 17         Cheapside     4.7
## 18        Pound.Hill     4.5
## 19        Gravel.Pit     3.5
## 20         Farm.Wood     5.1
# 4.2 Seleccionando filas aleatoriamente
# Usamos la funcion sample() para hacer esto: el default = FALSE
# hace el barajeo cada fila es seleccionada una vez
# hacemos de default replace = F para selecciona
# un numero unico de 8 de las 20 filas aleatoriamente:
worm[sample(1:20,8),]
##           Field.Name Area Slope Vegetation Soil.pH  Damp Worm.density
## 6           Oak.Mead  3.1     2  Grassland     3.9 FALSE            2
## 19        Gravel.Pit  2.9     1  Grassland     3.5 FALSE            1
## 18        Pound.Hill  4.4     2     Arable     4.5 FALSE            5
## 15        Pond.Field  4.1     0     Meadow     5.0  TRUE            6
## 5    Gunness.Thicket  3.8     0      Scrub     4.2 FALSE            6
## 11       Garden.Wood  2.9    10      Scrub     5.2 FALSE            8
## 16      Water.Meadow  3.9     0     Meadow     4.9  TRUE            8
## 14 Observatory.Ridge  1.8     6  Grassland     3.8 FALSE            0
# en resumen
worm[3,]#selecciona todas la columnas de la fila 3
##      Field.Name Area Slope Vegetation Soil.pH  Damp Worm.density
## 3 Nursery.Field  2.8     3  Grassland     4.3 FALSE            2
worm[-3,]# elimina toda la fila 3
##           Field.Name Area Slope Vegetation Soil.pH  Damp Worm.density
## 1        Nashs.Field  3.6    11  Grassland     4.1 FALSE            4
## 2     Silwood.Bottom  5.1     2     Arable     5.2 FALSE            7
## 4        Rush.Meadow  2.4     5     Meadow     4.9  TRUE            5
## 5    Gunness.Thicket  3.8     0      Scrub     4.2 FALSE            6
## 6           Oak.Mead  3.1     2  Grassland     3.9 FALSE            2
## 7       Church.Field  3.5     3  Grassland     4.2 FALSE            3
## 8            Ashurst  2.1     0     Arable     4.8 FALSE            4
## 9        The.Orchard  1.9     0    Orchard     5.7 FALSE            9
## 10     Rookery.Slope  1.5     4  Grassland     5.0  TRUE            7
## 11       Garden.Wood  2.9    10      Scrub     5.2 FALSE            8
## 12      North.Gravel  3.3     1  Grassland     4.1 FALSE            1
## 13      South.Gravel  3.7     2  Grassland     4.0 FALSE            2
## 14 Observatory.Ridge  1.8     6  Grassland     3.8 FALSE            0
## 15        Pond.Field  4.1     0     Meadow     5.0  TRUE            6
## 16      Water.Meadow  3.9     0     Meadow     4.9  TRUE            8
## 17         Cheapside  2.2     8      Scrub     4.7  TRUE            4
## 18        Pound.Hill  4.4     2     Arable     4.5 FALSE            5
## 19        Gravel.Pit  2.9     1  Grassland     3.5 FALSE            1
## 20         Farm.Wood  0.8    10      Scrub     5.1  TRUE            3
worm[1:3,]# selecciona todas las columnas de la fila 1 a la 3
##       Field.Name Area Slope Vegetation Soil.pH  Damp Worm.density
## 1    Nashs.Field  3.6    11  Grassland     4.1 FALSE            4
## 2 Silwood.Bottom  5.1     2     Arable     5.2 FALSE            7
## 3  Nursery.Field  2.8     3  Grassland     4.3 FALSE            2
worm[-(1:3),]# elimina las filas 1 al 3 de todas las columnas
##           Field.Name Area Slope Vegetation Soil.pH  Damp Worm.density
## 4        Rush.Meadow  2.4     5     Meadow     4.9  TRUE            5
## 5    Gunness.Thicket  3.8     0      Scrub     4.2 FALSE            6
## 6           Oak.Mead  3.1     2  Grassland     3.9 FALSE            2
## 7       Church.Field  3.5     3  Grassland     4.2 FALSE            3
## 8            Ashurst  2.1     0     Arable     4.8 FALSE            4
## 9        The.Orchard  1.9     0    Orchard     5.7 FALSE            9
## 10     Rookery.Slope  1.5     4  Grassland     5.0  TRUE            7
## 11       Garden.Wood  2.9    10      Scrub     5.2 FALSE            8
## 12      North.Gravel  3.3     1  Grassland     4.1 FALSE            1
## 13      South.Gravel  3.7     2  Grassland     4.0 FALSE            2
## 14 Observatory.Ridge  1.8     6  Grassland     3.8 FALSE            0
## 15        Pond.Field  4.1     0     Meadow     5.0  TRUE            6
## 16      Water.Meadow  3.9     0     Meadow     4.9  TRUE            8
## 17         Cheapside  2.2     8      Scrub     4.7  TRUE            4
## 18        Pound.Hill  4.4     2     Arable     4.5 FALSE            5
## 19        Gravel.Pit  2.9     1  Grassland     3.5 FALSE            1
## 20         Farm.Wood  0.8    10      Scrub     5.1  TRUE            3
worm[c(2,3,5),]# selecciona las filas 2,3,5 de todas las columnas
##        Field.Name Area Slope Vegetation Soil.pH  Damp Worm.density
## 2  Silwood.Bottom  5.1     2     Arable     5.2 FALSE            7
## 3   Nursery.Field  2.8     3  Grassland     4.3 FALSE            2
## 5 Gunness.Thicket  3.8     0      Scrub     4.2 FALSE            6
worm[,-3]# elimina la columna 3 de todas las filas
##           Field.Name Area Vegetation Soil.pH  Damp Worm.density
## 1        Nashs.Field  3.6  Grassland     4.1 FALSE            4
## 2     Silwood.Bottom  5.1     Arable     5.2 FALSE            7
## 3      Nursery.Field  2.8  Grassland     4.3 FALSE            2
## 4        Rush.Meadow  2.4     Meadow     4.9  TRUE            5
## 5    Gunness.Thicket  3.8      Scrub     4.2 FALSE            6
## 6           Oak.Mead  3.1  Grassland     3.9 FALSE            2
## 7       Church.Field  3.5  Grassland     4.2 FALSE            3
## 8            Ashurst  2.1     Arable     4.8 FALSE            4
## 9        The.Orchard  1.9    Orchard     5.7 FALSE            9
## 10     Rookery.Slope  1.5  Grassland     5.0  TRUE            7
## 11       Garden.Wood  2.9      Scrub     5.2 FALSE            8
## 12      North.Gravel  3.3  Grassland     4.1 FALSE            1
## 13      South.Gravel  3.7  Grassland     4.0 FALSE            2
## 14 Observatory.Ridge  1.8  Grassland     3.8 FALSE            0
## 15        Pond.Field  4.1     Meadow     5.0  TRUE            6
## 16      Water.Meadow  3.9     Meadow     4.9  TRUE            8
## 17         Cheapside  2.2      Scrub     4.7  TRUE            4
## 18        Pound.Hill  4.4     Arable     4.5 FALSE            5
## 19        Gravel.Pit  2.9  Grassland     3.5 FALSE            1
## 20         Farm.Wood  0.8      Scrub     5.1  TRUE            3
worm[,2:4]# selecciona las columnas de la 2 a la 4 de todas las filas
##    Area Slope Vegetation
## 1   3.6    11  Grassland
## 2   5.1     2     Arable
## 3   2.8     3  Grassland
## 4   2.4     5     Meadow
## 5   3.8     0      Scrub
## 6   3.1     2  Grassland
## 7   3.5     3  Grassland
## 8   2.1     0     Arable
## 9   1.9     0    Orchard
## 10  1.5     4  Grassland
## 11  2.9    10      Scrub
## 12  3.3     1  Grassland
## 13  3.7     2  Grassland
## 14  1.8     6  Grassland
## 15  4.1     0     Meadow
## 16  3.9     0     Meadow
## 17  2.2     8      Scrub
## 18  4.4     2     Arable
## 19  2.9     1  Grassland
## 20  0.8    10      Scrub
worm[,-(2:4)]# elimina las columnas de la 2 a la 4 de todas las filas
##           Field.Name Soil.pH  Damp Worm.density
## 1        Nashs.Field     4.1 FALSE            4
## 2     Silwood.Bottom     5.2 FALSE            7
## 3      Nursery.Field     4.3 FALSE            2
## 4        Rush.Meadow     4.9  TRUE            5
## 5    Gunness.Thicket     4.2 FALSE            6
## 6           Oak.Mead     3.9 FALSE            2
## 7       Church.Field     4.2 FALSE            3
## 8            Ashurst     4.8 FALSE            4
## 9        The.Orchard     5.7 FALSE            9
## 10     Rookery.Slope     5.0  TRUE            7
## 11       Garden.Wood     5.2 FALSE            8
## 12      North.Gravel     4.1 FALSE            1
## 13      South.Gravel     4.0 FALSE            2
## 14 Observatory.Ridge     3.8 FALSE            0
## 15        Pond.Field     5.0  TRUE            6
## 16      Water.Meadow     4.9  TRUE            8
## 17         Cheapside     4.7  TRUE            4
## 18        Pound.Hill     4.5 FALSE            5
## 19        Gravel.Pit     3.5 FALSE            1
## 20         Farm.Wood     5.1  TRUE            3
worm[,c(2,4,5)]# selecciona las columnas 2,4,5 de todas las filas
##    Area Vegetation Soil.pH
## 1   3.6  Grassland     4.1
## 2   5.1     Arable     5.2
## 3   2.8  Grassland     4.3
## 4   2.4     Meadow     4.9
## 5   3.8      Scrub     4.2
## 6   3.1  Grassland     3.9
## 7   3.5  Grassland     4.2
## 8   2.1     Arable     4.8
## 9   1.9    Orchard     5.7
## 10  1.5  Grassland     5.0
## 11  2.9      Scrub     5.2
## 12  3.3  Grassland     4.1
## 13  3.7  Grassland     4.0
## 14  1.8  Grassland     3.8
## 15  4.1     Meadow     5.0
## 16  3.9     Meadow     4.9
## 17  2.2      Scrub     4.7
## 18  4.4     Arable     4.5
## 19  2.9  Grassland     3.5
## 20  0.8      Scrub     5.1
worm[,c(1:5,4,5,6)]# aƱade duplicados
##           Field.Name Area Slope Vegetation Soil.pH Vegetation.1 Soil.pH.1
## 1        Nashs.Field  3.6    11  Grassland     4.1    Grassland       4.1
## 2     Silwood.Bottom  5.1     2     Arable     5.2       Arable       5.2
## 3      Nursery.Field  2.8     3  Grassland     4.3    Grassland       4.3
## 4        Rush.Meadow  2.4     5     Meadow     4.9       Meadow       4.9
## 5    Gunness.Thicket  3.8     0      Scrub     4.2        Scrub       4.2
## 6           Oak.Mead  3.1     2  Grassland     3.9    Grassland       3.9
## 7       Church.Field  3.5     3  Grassland     4.2    Grassland       4.2
## 8            Ashurst  2.1     0     Arable     4.8       Arable       4.8
## 9        The.Orchard  1.9     0    Orchard     5.7      Orchard       5.7
## 10     Rookery.Slope  1.5     4  Grassland     5.0    Grassland       5.0
## 11       Garden.Wood  2.9    10      Scrub     5.2        Scrub       5.2
## 12      North.Gravel  3.3     1  Grassland     4.1    Grassland       4.1
## 13      South.Gravel  3.7     2  Grassland     4.0    Grassland       4.0
## 14 Observatory.Ridge  1.8     6  Grassland     3.8    Grassland       3.8
## 15        Pond.Field  4.1     0     Meadow     5.0       Meadow       5.0
## 16      Water.Meadow  3.9     0     Meadow     4.9       Meadow       4.9
## 17         Cheapside  2.2     8      Scrub     4.7        Scrub       4.7
## 18        Pound.Hill  4.4     2     Arable     4.5       Arable       4.5
## 19        Gravel.Pit  2.9     1  Grassland     3.5    Grassland       3.5
## 20         Farm.Wood  0.8    10      Scrub     5.1        Scrub       5.1
##     Damp
## 1  FALSE
## 2  FALSE
## 3  FALSE
## 4   TRUE
## 5  FALSE
## 6  FALSE
## 7  FALSE
## 8  FALSE
## 9  FALSE
## 10  TRUE
## 11 FALSE
## 12 FALSE
## 13 FALSE
## 14 FALSE
## 15  TRUE
## 16  TRUE
## 17  TRUE
## 18 FALSE
## 19 FALSE
## 20  TRUE
# 4.3 Ordenando dataframes
worm[order(worm$Slope),]#descendiente
##           Field.Name Area Slope Vegetation Soil.pH  Damp Worm.density
## 5    Gunness.Thicket  3.8     0      Scrub     4.2 FALSE            6
## 8            Ashurst  2.1     0     Arable     4.8 FALSE            4
## 9        The.Orchard  1.9     0    Orchard     5.7 FALSE            9
## 15        Pond.Field  4.1     0     Meadow     5.0  TRUE            6
## 16      Water.Meadow  3.9     0     Meadow     4.9  TRUE            8
## 12      North.Gravel  3.3     1  Grassland     4.1 FALSE            1
## 19        Gravel.Pit  2.9     1  Grassland     3.5 FALSE            1
## 2     Silwood.Bottom  5.1     2     Arable     5.2 FALSE            7
## 6           Oak.Mead  3.1     2  Grassland     3.9 FALSE            2
## 13      South.Gravel  3.7     2  Grassland     4.0 FALSE            2
## 18        Pound.Hill  4.4     2     Arable     4.5 FALSE            5
## 3      Nursery.Field  2.8     3  Grassland     4.3 FALSE            2
## 7       Church.Field  3.5     3  Grassland     4.2 FALSE            3
## 10     Rookery.Slope  1.5     4  Grassland     5.0  TRUE            7
## 4        Rush.Meadow  2.4     5     Meadow     4.9  TRUE            5
## 14 Observatory.Ridge  1.8     6  Grassland     3.8 FALSE            0
## 17         Cheapside  2.2     8      Scrub     4.7  TRUE            4
## 11       Garden.Wood  2.9    10      Scrub     5.2 FALSE            8
## 20         Farm.Wood  0.8    10      Scrub     5.1  TRUE            3
## 1        Nashs.Field  3.6    11  Grassland     4.1 FALSE            4
worm[rev(order(worm$Slope)),]# ascendiente
##           Field.Name Area Slope Vegetation Soil.pH  Damp Worm.density
## 1        Nashs.Field  3.6    11  Grassland     4.1 FALSE            4
## 20         Farm.Wood  0.8    10      Scrub     5.1  TRUE            3
## 11       Garden.Wood  2.9    10      Scrub     5.2 FALSE            8
## 17         Cheapside  2.2     8      Scrub     4.7  TRUE            4
## 14 Observatory.Ridge  1.8     6  Grassland     3.8 FALSE            0
## 4        Rush.Meadow  2.4     5     Meadow     4.9  TRUE            5
## 10     Rookery.Slope  1.5     4  Grassland     5.0  TRUE            7
## 7       Church.Field  3.5     3  Grassland     4.2 FALSE            3
## 3      Nursery.Field  2.8     3  Grassland     4.3 FALSE            2
## 18        Pound.Hill  4.4     2     Arable     4.5 FALSE            5
## 13      South.Gravel  3.7     2  Grassland     4.0 FALSE            2
## 6           Oak.Mead  3.1     2  Grassland     3.9 FALSE            2
## 2     Silwood.Bottom  5.1     2     Arable     5.2 FALSE            7
## 19        Gravel.Pit  2.9     1  Grassland     3.5 FALSE            1
## 12      North.Gravel  3.3     1  Grassland     4.1 FALSE            1
## 16      Water.Meadow  3.9     0     Meadow     4.9  TRUE            8
## 15        Pond.Field  4.1     0     Meadow     5.0  TRUE            6
## 9        The.Orchard  1.9     0    Orchard     5.7 FALSE            9
## 8            Ashurst  2.1     0     Arable     4.8 FALSE            4
## 5    Gunness.Thicket  3.8     0      Scrub     4.2 FALSE            6
worm[order(worm$Vegetation,worm$Worm.density,worm$Soil.pH),]
##           Field.Name Area Slope Vegetation Soil.pH  Damp Worm.density
## 8            Ashurst  2.1     0     Arable     4.8 FALSE            4
## 18        Pound.Hill  4.4     2     Arable     4.5 FALSE            5
## 2     Silwood.Bottom  5.1     2     Arable     5.2 FALSE            7
## 14 Observatory.Ridge  1.8     6  Grassland     3.8 FALSE            0
## 19        Gravel.Pit  2.9     1  Grassland     3.5 FALSE            1
## 12      North.Gravel  3.3     1  Grassland     4.1 FALSE            1
## 6           Oak.Mead  3.1     2  Grassland     3.9 FALSE            2
## 13      South.Gravel  3.7     2  Grassland     4.0 FALSE            2
## 3      Nursery.Field  2.8     3  Grassland     4.3 FALSE            2
## 7       Church.Field  3.5     3  Grassland     4.2 FALSE            3
## 1        Nashs.Field  3.6    11  Grassland     4.1 FALSE            4
## 10     Rookery.Slope  1.5     4  Grassland     5.0  TRUE            7
## 4        Rush.Meadow  2.4     5     Meadow     4.9  TRUE            5
## 15        Pond.Field  4.1     0     Meadow     5.0  TRUE            6
## 16      Water.Meadow  3.9     0     Meadow     4.9  TRUE            8
## 9        The.Orchard  1.9     0    Orchard     5.7 FALSE            9
## 20         Farm.Wood  0.8    10      Scrub     5.1  TRUE            3
## 17         Cheapside  2.2     8      Scrub     4.7  TRUE            4
## 5    Gunness.Thicket  3.8     0      Scrub     4.2 FALSE            6
## 11       Garden.Wood  2.9    10      Scrub     5.2 FALSE            8
# si queremos vegetation,worm.density,
# soil pH y slope, y las queremos en el orden desde la izquierda
# a derecha, especificamos los numeros de las columnas en la
# secuencia que queremos que aparezcan como un vector: c(4,7,5,3):
worm[order(worm$Vegetation,worm$Worm.density),c(4,7,5,3)]
##    Vegetation Worm.density Soil.pH Slope
## 8      Arable            4     4.8     0
## 18     Arable            5     4.5     2
## 2      Arable            7     5.2     2
## 14  Grassland            0     3.8     6
## 12  Grassland            1     4.1     1
## 19  Grassland            1     3.5     1
## 3   Grassland            2     4.3     3
## 6   Grassland            2     3.9     2
## 13  Grassland            2     4.0     2
## 7   Grassland            3     4.2     3
## 1   Grassland            4     4.1    11
## 10  Grassland            7     5.0     4
## 4      Meadow            5     4.9     5
## 15     Meadow            6     5.0     0
## 16     Meadow            8     4.9     0
## 9     Orchard            9     5.7     0
## 20      Scrub            3     5.1    10
## 17      Scrub            4     4.7     8
## 5       Scrub            6     4.2     0
## 11      Scrub            8     5.2    10
# 4.4 Usando condiciones logicas para seleccionar filas
worm[worm$Damp==T,]
##       Field.Name Area Slope Vegetation Soil.pH Damp Worm.density
## 4    Rush.Meadow  2.4     5     Meadow     4.9 TRUE            5
## 10 Rookery.Slope  1.5     4  Grassland     5.0 TRUE            7
## 15    Pond.Field  4.1     0     Meadow     5.0 TRUE            6
## 16  Water.Meadow  3.9     0     Meadow     4.9 TRUE            8
## 17     Cheapside  2.2     8      Scrub     4.7 TRUE            4
## 20     Farm.Wood  0.8    10      Scrub     5.1 TRUE            3
worm[worm$Worm.density>median(worm$Worm.density)&worm$Soil.pH<5.2,]
##         Field.Name Area Slope Vegetation Soil.pH  Damp Worm.density
## 4      Rush.Meadow  2.4     5     Meadow     4.9  TRUE            5
## 5  Gunness.Thicket  3.8     0      Scrub     4.2 FALSE            6
## 10   Rookery.Slope  1.5     4  Grassland     5.0  TRUE            7
## 15      Pond.Field  4.1     0     Meadow     5.0  TRUE            6
## 16    Water.Meadow  3.9     0     Meadow     4.9  TRUE            8
## 18      Pound.Hill  4.4     2     Arable     4.5 FALSE            5
# Suponga that que queremos extraer todas las columnas que contienen numeros
# en vez de caracteres o variables logicas
# podemos aplicar la funcion is.numeric 
# a traves de todas las columnas
# usando sapply para crear los indices asi:
worm[,sapply(worm, is.numeric)]
##    Area Slope Soil.pH Worm.density
## 1   3.6    11     4.1            4
## 2   5.1     2     5.2            7
## 3   2.8     3     4.3            2
## 4   2.4     5     4.9            5
## 5   3.8     0     4.2            6
## 6   3.1     2     3.9            2
## 7   3.5     3     4.2            3
## 8   2.1     0     4.8            4
## 9   1.9     0     5.7            9
## 10  1.5     4     5.0            7
## 11  2.9    10     5.2            8
## 12  3.3     1     4.1            1
## 13  3.7     2     4.0            2
## 14  1.8     6     3.8            0
## 15  4.1     0     5.0            6
## 16  3.9     0     4.9            8
## 17  2.2     8     4.7            4
## 18  4.4     2     4.5            5
## 19  2.9     1     3.5            1
## 20  0.8    10     5.1            3
#con la funcion is.factor()
worm[,sapply(worm, is.factor)]
##           Field.Name Vegetation
## 1        Nashs.Field  Grassland
## 2     Silwood.Bottom     Arable
## 3      Nursery.Field  Grassland
## 4        Rush.Meadow     Meadow
## 5    Gunness.Thicket      Scrub
## 6           Oak.Mead  Grassland
## 7       Church.Field  Grassland
## 8            Ashurst     Arable
## 9        The.Orchard    Orchard
## 10     Rookery.Slope  Grassland
## 11       Garden.Wood      Scrub
## 12      North.Gravel  Grassland
## 13      South.Gravel  Grassland
## 14 Observatory.Ridge  Grassland
## 15        Pond.Field     Meadow
## 16      Water.Meadow     Meadow
## 17         Cheapside      Scrub
## 18        Pound.Hill     Arable
## 19        Gravel.Pit  Grassland
## 20         Farm.Wood      Scrub
# To drop a row or rows from the dataframe, 
# use negative subscripts. Thus to drop the middle 10 rows 
worm[(6:15),]                                                                                                   
##           Field.Name Area Slope Vegetation Soil.pH  Damp Worm.density
## 6           Oak.Mead  3.1     2  Grassland     3.9 FALSE            2
## 7       Church.Field  3.5     3  Grassland     4.2 FALSE            3
## 8            Ashurst  2.1     0     Arable     4.8 FALSE            4
## 9        The.Orchard  1.9     0    Orchard     5.7 FALSE            9
## 10     Rookery.Slope  1.5     4  Grassland     5.0  TRUE            7
## 11       Garden.Wood  2.9    10      Scrub     5.2 FALSE            8
## 12      North.Gravel  3.3     1  Grassland     4.1 FALSE            1
## 13      South.Gravel  3.7     2  Grassland     4.0 FALSE            2
## 14 Observatory.Ridge  1.8     6  Grassland     3.8 FALSE            0
## 15        Pond.Field  4.1     0     Meadow     5.0  TRUE            6
# todas las filas que no son grasslands 
# ( ! significa NOT):
worm[!(worm$Vegetation=="Grassland"),]
##         Field.Name Area Slope Vegetation Soil.pH  Damp Worm.density
## 2   Silwood.Bottom  5.1     2     Arable     5.2 FALSE            7
## 4      Rush.Meadow  2.4     5     Meadow     4.9  TRUE            5
## 5  Gunness.Thicket  3.8     0      Scrub     4.2 FALSE            6
## 8          Ashurst  2.1     0     Arable     4.8 FALSE            4
## 9      The.Orchard  1.9     0    Orchard     5.7 FALSE            9
## 11     Garden.Wood  2.9    10      Scrub     5.2 FALSE            8
## 15      Pond.Field  4.1     0     Meadow     5.0  TRUE            6
## 16    Water.Meadow  3.9     0     Meadow     4.9  TRUE            8
## 17       Cheapside  2.2     8      Scrub     4.7  TRUE            4
## 18      Pound.Hill  4.4     2     Arable     4.5 FALSE            5
## 20       Farm.Wood  0.8    10      Scrub     5.1  TRUE            3
worm[(worm$Vegetation=="Grassland"),]
##           Field.Name Area Slope Vegetation Soil.pH  Damp Worm.density
## 1        Nashs.Field  3.6    11  Grassland     4.1 FALSE            4
## 3      Nursery.Field  2.8     3  Grassland     4.3 FALSE            2
## 6           Oak.Mead  3.1     2  Grassland     3.9 FALSE            2
## 7       Church.Field  3.5     3  Grassland     4.2 FALSE            3
## 10     Rookery.Slope  1.5     4  Grassland     5.0  TRUE            7
## 12      North.Gravel  3.3     1  Grassland     4.1 FALSE            1
## 13      South.Gravel  3.7     2  Grassland     4.0 FALSE            2
## 14 Observatory.Ridge  1.8     6  Grassland     3.8 FALSE            0
## 19        Gravel.Pit  2.9     1  Grassland     3.5 FALSE            1
worm[(worm$Vegetation=="Grassland") & worm$Damp==T,]
##       Field.Name Area Slope Vegetation Soil.pH Damp Worm.density
## 10 Rookery.Slope  1.5     4  Grassland       5 TRUE            7
worm[which(worm$Damp==F),]
##           Field.Name Area Slope Vegetation Soil.pH  Damp Worm.density
## 1        Nashs.Field  3.6    11  Grassland     4.1 FALSE            4
## 2     Silwood.Bottom  5.1     2     Arable     5.2 FALSE            7
## 3      Nursery.Field  2.8     3  Grassland     4.3 FALSE            2
## 5    Gunness.Thicket  3.8     0      Scrub     4.2 FALSE            6
## 6           Oak.Mead  3.1     2  Grassland     3.9 FALSE            2
## 7       Church.Field  3.5     3  Grassland     4.2 FALSE            3
## 8            Ashurst  2.1     0     Arable     4.8 FALSE            4
## 9        The.Orchard  1.9     0    Orchard     5.7 FALSE            9
## 11       Garden.Wood  2.9    10      Scrub     5.2 FALSE            8
## 12      North.Gravel  3.3     1  Grassland     4.1 FALSE            1
## 13      South.Gravel  3.7     2  Grassland     4.0 FALSE            2
## 14 Observatory.Ridge  1.8     6  Grassland     3.8 FALSE            0
## 18        Pound.Hill  4.4     2     Arable     4.5 FALSE            5
## 19        Gravel.Pit  2.9     1  Grassland     3.5 FALSE            1
worm[which(worm$Damp==T),]
##       Field.Name Area Slope Vegetation Soil.pH Damp Worm.density
## 4    Rush.Meadow  2.4     5     Meadow     4.9 TRUE            5
## 10 Rookery.Slope  1.5     4  Grassland     5.0 TRUE            7
## 15    Pond.Field  4.1     0     Meadow     5.0 TRUE            6
## 16  Water.Meadow  3.9     0     Meadow     4.9 TRUE            8
## 17     Cheapside  2.2     8      Scrub     4.7 TRUE            4
## 20     Farm.Wood  0.8    10      Scrub     5.1 TRUE            3
# 4.5 Omitiendo filas que contienen, NA
worm.omit <- read.table("C:\\Users\\Luis\\Documents\\therbook\\worms.missing.txt",
                   header = TRUE,sep = "")
worm.omit
##           Field.Name Area Slope Vegetation Soil.pH  Damp Worm.density
## 1        Nashs.Field  3.6    11  Grassland     4.1 FALSE            4
## 2     Silwood.Bottom  5.1    NA     Arable     5.2 FALSE            7
## 3      Nursery.Field  2.8     3  Grassland     4.3 FALSE            2
## 4        Rush.Meadow  2.4     5     Meadow     4.9  TRUE            5
## 5    Gunness.Thicket  3.8     0      Scrub     4.2 FALSE            6
## 6           Oak.Mead  3.1     2  Grassland     3.9 FALSE            2
## 7       Church.Field  3.5     3  Grassland      NA    NA           NA
## 8            Ashurst  2.1     0     Arable     4.8 FALSE            4
## 9        The.Orchard  1.9     0    Orchard     5.7 FALSE            9
## 10     Rookery.Slope  1.5     4  Grassland     5.0  TRUE            7
## 11       Garden.Wood  2.9    10      Scrub     5.2 FALSE            8
## 12      North.Gravel  3.3     1  Grassland     4.1 FALSE            1
## 13      South.Gravel  3.7     2  Grassland     4.0 FALSE            2
## 14 Observatory.Ridge  1.8     6  Grassland     3.8 FALSE            0
## 15        Pond.Field  4.1     0     Meadow     5.0  TRUE            6
## 16      Water.Meadow  3.9     0     Meadow     4.9  TRUE            8
## 17         Cheapside  2.2     8      Scrub     4.7  TRUE            4
## 18        Pound.Hill  4.4     2     Arable     4.5 FALSE            5
## 19        Gravel.Pit   NA     1  Grassland     3.5 FALSE            1
## 20         Farm.Wood  0.8    10      Scrub     5.1  TRUE            3
na.omit(worm.omit)
##           Field.Name Area Slope Vegetation Soil.pH  Damp Worm.density
## 1        Nashs.Field  3.6    11  Grassland     4.1 FALSE            4
## 3      Nursery.Field  2.8     3  Grassland     4.3 FALSE            2
## 4        Rush.Meadow  2.4     5     Meadow     4.9  TRUE            5
## 5    Gunness.Thicket  3.8     0      Scrub     4.2 FALSE            6
## 6           Oak.Mead  3.1     2  Grassland     3.9 FALSE            2
## 8            Ashurst  2.1     0     Arable     4.8 FALSE            4
## 9        The.Orchard  1.9     0    Orchard     5.7 FALSE            9
## 10     Rookery.Slope  1.5     4  Grassland     5.0  TRUE            7
## 11       Garden.Wood  2.9    10      Scrub     5.2 FALSE            8
## 12      North.Gravel  3.3     1  Grassland     4.1 FALSE            1
## 13      South.Gravel  3.7     2  Grassland     4.0 FALSE            2
## 14 Observatory.Ridge  1.8     6  Grassland     3.8 FALSE            0
## 15        Pond.Field  4.1     0     Meadow     5.0  TRUE            6
## 16      Water.Meadow  3.9     0     Meadow     4.9  TRUE            8
## 17         Cheapside  2.2     8      Scrub     4.7  TRUE            4
## 18        Pound.Hill  4.4     2     Arable     4.5 FALSE            5
## 20         Farm.Wood  0.8    10      Scrub     5.1  TRUE            3
complete.cases(worm.omit)
##  [1]  TRUE FALSE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE
## [12]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE
# 4.7 Oredenamiento complejo con direcciones mixtas
# Algunas veces hay  multiples variables para ordenar,
# pero las variables deben ser ordenadas en direcciones opuestas.
# este ejemplo, se requiere ordenar la data
# primero por Vegetation en oden alfabetico (el default) y
# despues entre cada tipo de Vegetation ordenar por Worm.density
# en orden decreciente (densidades altas primero). El
# truco aqui esta en usar order (en vez de rev(order))
# pero poner un signo menos en frente de Worm.density
# Asi:
worm[order(worm$Vegetation,-worm$Worm.density),]
##           Field.Name Area Slope Vegetation Soil.pH  Damp Worm.density
## 2     Silwood.Bottom  5.1     2     Arable     5.2 FALSE            7
## 18        Pound.Hill  4.4     2     Arable     4.5 FALSE            5
## 8            Ashurst  2.1     0     Arable     4.8 FALSE            4
## 10     Rookery.Slope  1.5     4  Grassland     5.0  TRUE            7
## 1        Nashs.Field  3.6    11  Grassland     4.1 FALSE            4
## 7       Church.Field  3.5     3  Grassland     4.2 FALSE            3
## 3      Nursery.Field  2.8     3  Grassland     4.3 FALSE            2
## 6           Oak.Mead  3.1     2  Grassland     3.9 FALSE            2
## 13      South.Gravel  3.7     2  Grassland     4.0 FALSE            2
## 12      North.Gravel  3.3     1  Grassland     4.1 FALSE            1
## 19        Gravel.Pit  2.9     1  Grassland     3.5 FALSE            1
## 14 Observatory.Ridge  1.8     6  Grassland     3.8 FALSE            0
## 16      Water.Meadow  3.9     0     Meadow     4.9  TRUE            8
## 15        Pond.Field  4.1     0     Meadow     5.0  TRUE            6
## 4        Rush.Meadow  2.4     5     Meadow     4.9  TRUE            5
## 9        The.Orchard  1.9     0    Orchard     5.7 FALSE            9
## 11       Garden.Wood  2.9    10      Scrub     5.2 FALSE            8
## 5    Gunness.Thicket  3.8     0      Scrub     4.2 FALSE            6
## 17         Cheapside  2.2     8      Scrub     4.7  TRUE            4
## 20         Farm.Wood  0.8    10      Scrub     5.1  TRUE            3
# el uso del signo menos solo funciona cuando se esta ordenando 
# variables numericas.
# para niveles de factores use la funcion rank
# para hacer los niveles numericos asi:
worm[order(-rank(worm$Vegetation,-worm$Worm.density)),]
##           Field.Name Area Slope Vegetation Soil.pH  Damp Worm.density
## 5    Gunness.Thicket  3.8     0      Scrub     4.2 FALSE            6
## 11       Garden.Wood  2.9    10      Scrub     5.2 FALSE            8
## 17         Cheapside  2.2     8      Scrub     4.7  TRUE            4
## 20         Farm.Wood  0.8    10      Scrub     5.1  TRUE            3
## 9        The.Orchard  1.9     0    Orchard     5.7 FALSE            9
## 4        Rush.Meadow  2.4     5     Meadow     4.9  TRUE            5
## 15        Pond.Field  4.1     0     Meadow     5.0  TRUE            6
## 16      Water.Meadow  3.9     0     Meadow     4.9  TRUE            8
## 1        Nashs.Field  3.6    11  Grassland     4.1 FALSE            4
## 3      Nursery.Field  2.8     3  Grassland     4.3 FALSE            2
## 6           Oak.Mead  3.1     2  Grassland     3.9 FALSE            2
## 7       Church.Field  3.5     3  Grassland     4.2 FALSE            3
## 10     Rookery.Slope  1.5     4  Grassland     5.0  TRUE            7
## 12      North.Gravel  3.3     1  Grassland     4.1 FALSE            1
## 13      South.Gravel  3.7     2  Grassland     4.0 FALSE            2
## 14 Observatory.Ridge  1.8     6  Grassland     3.8 FALSE            0
## 19        Gravel.Pit  2.9     1  Grassland     3.5 FALSE            1
## 2     Silwood.Bottom  5.1     2     Arable     5.2 FALSE            7
## 8            Ashurst  2.1     0     Arable     4.8 FALSE            4
## 18        Pound.Hill  4.4     2     Arable     4.5 FALSE            5
# 4.12 Usando la funcion match en dataframes
unique(worm$Vegetation)
## [1] Grassland Arable    Meadow    Scrub     Orchard  
## Levels: Arable Grassland Meadow Orchard Scrub
# y queremos saber el herbicida apropiado a usar
# en c/u de los 20 campos. Los herbicidas estan en otra dataframe
# que contiene las recomendaciones para 
# para una comunidad de plantas mucho mayor
herbicidas <- read.table("C:\\Users\\Luis\\Documents\\therbook\\herbicides.txt",header = TRUE)
herbicidas
##         Type Herbicide
## 1   Woodland  Fusilade
## 2    Conifer  Weedwipe
## 3     Arable  Twinspan
## 4       Hill  Weedwipe
## 5    Bracken  Fusilade
## 6      Scrub  Weedwipe
## 7  Grassland  Allclear
## 8      Chalk  Vanquish
## 9     Meadow  Propinol
## 10      Lawn  Vanquish
## 11   Orchard  Fusilade
## 12     Verge  Allclear
herbicidas$Herbicide[match(worm$Vegetation,herbicidas$Type)]
##  [1] Allclear Twinspan Allclear Propinol Weedwipe Allclear Allclear
##  [8] Twinspan Fusilade Allclear Weedwipe Allclear Allclear Allclear
## [15] Propinol Propinol Weedwipe Twinspan Allclear Weedwipe
## Levels: Allclear Fusilade Propinol Twinspan Vanquish Weedwipe
worm$hb<-herbicidas$Herbicide[match(worm$Vegetation,herbicidas$Type)]
worm
##           Field.Name Area Slope Vegetation Soil.pH  Damp Worm.density
## 1        Nashs.Field  3.6    11  Grassland     4.1 FALSE            4
## 2     Silwood.Bottom  5.1     2     Arable     5.2 FALSE            7
## 3      Nursery.Field  2.8     3  Grassland     4.3 FALSE            2
## 4        Rush.Meadow  2.4     5     Meadow     4.9  TRUE            5
## 5    Gunness.Thicket  3.8     0      Scrub     4.2 FALSE            6
## 6           Oak.Mead  3.1     2  Grassland     3.9 FALSE            2
## 7       Church.Field  3.5     3  Grassland     4.2 FALSE            3
## 8            Ashurst  2.1     0     Arable     4.8 FALSE            4
## 9        The.Orchard  1.9     0    Orchard     5.7 FALSE            9
## 10     Rookery.Slope  1.5     4  Grassland     5.0  TRUE            7
## 11       Garden.Wood  2.9    10      Scrub     5.2 FALSE            8
## 12      North.Gravel  3.3     1  Grassland     4.1 FALSE            1
## 13      South.Gravel  3.7     2  Grassland     4.0 FALSE            2
## 14 Observatory.Ridge  1.8     6  Grassland     3.8 FALSE            0
## 15        Pond.Field  4.1     0     Meadow     5.0  TRUE            6
## 16      Water.Meadow  3.9     0     Meadow     4.9  TRUE            8
## 17         Cheapside  2.2     8      Scrub     4.7  TRUE            4
## 18        Pound.Hill  4.4     2     Arable     4.5 FALSE            5
## 19        Gravel.Pit  2.9     1  Grassland     3.5 FALSE            1
## 20         Farm.Wood  0.8    10      Scrub     5.1  TRUE            3
##          hb
## 1  Allclear
## 2  Twinspan
## 3  Allclear
## 4  Propinol
## 5  Weedwipe
## 6  Allclear
## 7  Allclear
## 8  Twinspan
## 9  Fusilade
## 10 Allclear
## 11 Weedwipe
## 12 Allclear
## 13 Allclear
## 14 Allclear
## 15 Propinol
## 16 Propinol
## 17 Weedwipe
## 18 Twinspan
## 19 Allclear
## 20 Weedwipe