1. Hoja 1

1.1 Usando dplyr

Usando la función set.seed(20 ) y colocando sus dos últimos números de cédula en los espacios con guiones al piso, realice las siguientes actividades:

ACTIVIDAD 1

  • Genere una muestra de tamaño 120 de la distribución normal con media 5, desviación estándar 0.85. Use solo dos números decimales (rnorm()).
set.seed(2058)
mtra_1 <-rnorm(n=120, mean = 5, sd = 0.85)
norm_1<-round(mtra_1,digits = 2)
norm_1
##   [1] 4.60 4.41 4.49 5.75 4.61 6.15 5.44 5.15 4.72 5.47 4.66 3.11 3.26 5.73 4.24
##  [16] 4.39 4.17 6.26 4.33 4.04 4.55 4.12 4.80 5.32 6.45 4.39 5.57 4.73 3.40 5.76
##  [31] 5.76 5.41 3.39 4.02 5.51 5.03 5.47 4.75 5.92 5.99 4.14 6.13 5.98 4.54 5.29
##  [46] 4.59 4.49 4.50 6.00 4.89 4.94 5.43 4.40 4.05 2.89 4.66 3.98 4.61 5.40 5.50
##  [61] 3.64 5.99 3.82 3.98 5.96 6.75 3.92 4.25 4.25 4.62 5.38 4.08 6.18 4.77 5.57
##  [76] 4.08 3.78 4.97 4.67 5.80 4.85 6.03 5.57 4.15 4.48 6.00 4.89 5.98 3.96 5.62
##  [91] 4.95 6.33 5.44 4.64 5.52 4.80 6.11 5.42 4.66 5.20 4.01 4.47 5.25 4.56 3.97
## [106] 5.77 4.65 3.17 5.26 5.16 3.96 4.59 6.23 3.42 5.37 4.15 4.16 4.15 4.36 4.72
  • Genere una muestra de tamaño 120 de la distribución binomial con parámetros 0.8 (probabilidad) y 20 (ensayos independientes) (rbinom()).
mtra_2 <-rbinom(n = 120,prob = 0.8,size = 20)
mtra_2
##   [1] 10 15 12 18 16 19 18 18 15 15 13 17 16 16 17 14 17 16 16 19 16 14 20 17 15
##  [26] 11 13 15 17 15 16 18 16 16 16 18 16 16 18 12 15 17 11 18 17 16 19 18 18 16
##  [51] 18 17 17 12 15 18 11 20 16 16 16 18 16 20 15 15 14 18 18 15 17 16 15 13 17
##  [76] 14 14 17 16 17 17 18 13 17 19 17 17 13 17 17 18 15 15 13 15 16 15 16 17 16
## [101] 14 18 17 14 17 15 17 18 16 16 18 16 17 15 18 17 12 17 17 18
  • Genere una muestra de tamaño 120 de la distribución Poisson con parámetro 10.5 (media) (rpois()).
mtra_3 <-rpois(n = 120,lambda = 10.5)
mtra_3
##   [1] 11 14 12 14 10 11  7 14 11  6 12  8  5 13 13 12 14  6 14 10  7 10 12  8 16
##  [26]  8  9 10 13 11  6  8 12 11 10 11  9 16 13  9 14 11 12 11  8 10  8 11  7  6
##  [51]  4  5  5 16 12  9  4  9  7  5  8 14  7 10  5 10  9 13 13  9 12 11  6 10 16
##  [76] 14  8  7 16  4 11  9  2  6  9 10  9 15  7 10 12  4 19  7 13 12 11  8 13 10
## [101] 10 13  7 10  8 10  7  6  6  6  8  5  9  8 11  9 11 13  8  6
  • Genere una muestra con reemplazo de tamaño 120 de una secuencia de 300 números (sample.int()).
mtra_4 <-sample.int(n = 300, size =120, replace = TRUE, prob = NULL)
mtra_4
##   [1]  69  51  24 291  91  34 210 240  34 130  45   8  16 217 173  96 291 201
##  [19]  25  76 150 299 188  70 180 152 210  90 198  74 233 164 201 183 175 150
##  [37] 207 135  32 238 166 116 204 289 272  50 141 198 257  56  96  51 165 235
##  [55] 183 237 192 220  59  64 248 164 227 294  33 111 295 216 275 266 210 161
##  [73]  93  47 165 220 236  65 259 201  75  34  90 144 135 245 193 193 150  74
##  [91] 293 118 158 121 248 246   7 158  29 299 135 115 104 138 107 239 126 281
## [109]  69 162 282  70 142 191 113 268  92 278  12  85
  • Usando la librería purrr genere una muestra de la distribución de Bernoulli de tamaño 120 y parámetro 0.75 (probabilidad) (rbernoulli()) y cambie el FALSE ausente y el TRUE por presente.
mtra_5 <-rbernoulli(n = 120,p = 0.75)
mtra_5
##   [1]  TRUE  TRUE FALSE FALSE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE FALSE
##  [13]  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [25]  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE  TRUE FALSE  TRUE  TRUE FALSE
##  [37]  TRUE  TRUE  TRUE FALSE  TRUE FALSE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE
##  [49]  TRUE  TRUE  TRUE FALSE  TRUE FALSE FALSE  TRUE FALSE  TRUE  TRUE FALSE
##  [61]  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE  TRUE FALSE FALSE  TRUE  TRUE
##  [73]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE FALSE
##  [85]  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE
##  [97]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE  TRUE  TRUE FALSE
## [109]  TRUE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE
ber_1 <- ifelse(mtra_5 == TRUE, "Presente", "Ausente")
ber_1
##   [1] "Presente" "Presente" "Ausente"  "Ausente"  "Presente" "Presente"
##   [7] "Presente" "Ausente"  "Presente" "Presente" "Presente" "Ausente" 
##  [13] "Presente" "Presente" "Ausente"  "Presente" "Presente" "Presente"
##  [19] "Presente" "Presente" "Presente" "Presente" "Presente" "Presente"
##  [25] "Presente" "Presente" "Presente" "Presente" "Presente" "Ausente" 
##  [31] "Ausente"  "Presente" "Ausente"  "Presente" "Presente" "Ausente" 
##  [37] "Presente" "Presente" "Presente" "Ausente"  "Presente" "Ausente" 
##  [43] "Presente" "Presente" "Ausente"  "Presente" "Presente" "Presente"
##  [49] "Presente" "Presente" "Presente" "Ausente"  "Presente" "Ausente" 
##  [55] "Ausente"  "Presente" "Ausente"  "Presente" "Presente" "Ausente" 
##  [61] "Presente" "Presente" "Presente" "Presente" "Ausente"  "Ausente" 
##  [67] "Ausente"  "Presente" "Ausente"  "Ausente"  "Presente" "Presente"
##  [73] "Presente" "Presente" "Presente" "Presente" "Presente" "Presente"
##  [79] "Presente" "Presente" "Presente" "Ausente"  "Presente" "Ausente" 
##  [85] "Presente" "Presente" "Presente" "Presente" "Presente" "Ausente" 
##  [91] "Presente" "Presente" "Presente" "Ausente"  "Presente" "Presente"
##  [97] "Presente" "Presente" "Presente" "Presente" "Presente" "Presente"
## [103] "Presente" "Ausente"  "Ausente"  "Presente" "Presente" "Ausente" 
## [109] "Presente" "Ausente"  "Ausente"  "Presente" "Presente" "Presente"
## [115] "Presente" "Presente" "Presente" "Presente" "Presente" "Ausente"
  • Genere tres niveles de un factor, cada uno con 40 datos y etiqúetelos con S para identificar la planta sana, PA para las plantas parcialmente afectadas y MA para las muy afectadas. Use la función (gl()).
niveles <- gl(n = 3,k =40 ,length = 120,labels =c("S","PA","MA") )
niveles
##   [1] S  S  S  S  S  S  S  S  S  S  S  S  S  S  S  S  S  S  S  S  S  S  S  S  S 
##  [26] S  S  S  S  S  S  S  S  S  S  S  S  S  S  S  PA PA PA PA PA PA PA PA PA PA
##  [51] PA PA PA PA PA PA PA PA PA PA PA PA PA PA PA PA PA PA PA PA PA PA PA PA PA
##  [76] PA PA PA PA PA MA MA MA MA MA MA MA MA MA MA MA MA MA MA MA MA MA MA MA MA
## [101] MA MA MA MA MA MA MA MA MA MA MA MA MA MA MA MA MA MA MA MA
## Levels: S PA MA
  • Genere dos niveles de un factor usando la distribuciÓn uniforme con parámetros 0 y 1.2, ponga para cada datos a generar de los 120 la condición de si el nuúero generado es menor a 0.5, los etiquetamos como FO para asociarlo a fertilización orgánica, de lo contrario, use FI para asociarlo a fertilización inorgánica. Use la función (runif()). Si lo desea use la función (ifelse()) o (if else()) para condicionar.
mtra_6<- runif(n=120,min = 0, max = 1.2)
unif_1<-round(mtra_6,digits = 2)
unif_1
##   [1] 0.60 0.25 0.91 0.59 0.96 0.70 1.12 0.04 0.94 0.69 0.86 0.65 0.23 1.17 0.04
##  [16] 0.33 0.43 0.17 0.59 1.15 0.91 0.63 0.13 0.05 0.74 0.40 0.70 0.99 0.39 0.39
##  [31] 0.02 0.08 0.59 1.15 0.80 0.74 0.50 0.78 0.16 1.09 0.75 0.38 0.29 1.15 1.12
##  [46] 0.54 0.01 0.04 0.77 0.32 0.81 0.77 0.53 0.37 0.50 0.07 0.64 0.18 0.67 0.71
##  [61] 0.59 0.39 0.43 0.69 0.29 1.15 0.12 0.80 0.56 0.54 0.53 0.72 1.08 0.67 0.52
##  [76] 0.14 0.53 0.48 0.72 0.25 0.07 0.99 1.04 0.68 0.93 0.75 1.06 0.40 1.08 0.10
##  [91] 0.89 0.19 0.95 0.50 0.75 0.05 0.22 0.42 0.41 0.44 0.23 0.41 0.48 0.44 0.96
## [106] 1.20 0.49 0.51 0.97 1.12 1.09 0.03 0.40 0.13 0.49 0.20 0.05 0.34 0.59 0.06
fer_1<- ifelse(mtra_6 <0.5, "FO", "FI")
fer_1
##   [1] "FI" "FO" "FI" "FI" "FI" "FI" "FI" "FO" "FI" "FI" "FI" "FI" "FO" "FI" "FO"
##  [16] "FO" "FO" "FO" "FI" "FI" "FI" "FI" "FO" "FO" "FI" "FO" "FI" "FI" "FO" "FO"
##  [31] "FO" "FO" "FI" "FI" "FI" "FI" "FI" "FI" "FO" "FI" "FI" "FO" "FO" "FI" "FI"
##  [46] "FI" "FO" "FO" "FI" "FO" "FI" "FI" "FI" "FO" "FO" "FO" "FI" "FO" "FI" "FI"
##  [61] "FI" "FO" "FO" "FI" "FO" "FI" "FO" "FI" "FI" "FI" "FI" "FI" "FI" "FI" "FI"
##  [76] "FO" "FI" "FO" "FI" "FO" "FO" "FI" "FI" "FI" "FI" "FI" "FI" "FO" "FI" "FO"
##  [91] "FI" "FO" "FI" "FO" "FI" "FO" "FO" "FO" "FO" "FO" "FO" "FO" "FO" "FO" "FI"
## [106] "FI" "FO" "FI" "FI" "FI" "FI" "FO" "FO" "FO" "FO" "FO" "FO" "FO" "FI" "FO"

ACTIVIDAD 2

  • Construya un marco de datos (data.frame()) o una tableta (tibble()) con todas la variables antes generadas y asigne repectivamente los nombres de variable:Biomasa(gramos), Flores.r (conteo de flores en tres ramas), Flores.d (conteo de flores desprendidas), Hojas.d (conteo de hojas desprendidas), Plaga , Estatus y Fertlización.
tab_1 <- data.frame(Biomasa = norm_1, Flores.r = mtra_2, Flores.d=mtra_3, Hojas.d=mtra_4, Plaga=ber_1, Estatus=niveles, Fertilizacion=fer_1)
head(tab_1)
##   Biomasa Flores.r Flores.d Hojas.d    Plaga Estatus Fertilizacion
## 1    4.60       10       11      69 Presente       S            FI
## 2    4.41       15       14      51 Presente       S            FO
## 3    4.49       12       12      24  Ausente       S            FI
## 4    5.75       18       14     291  Ausente       S            FI
## 5    4.61       16       10      91 Presente       S            FI
## 6    6.15       19       11      34 Presente       S            FI
  • Revise del objeto creado su dimensión (dim()), su estructura con (str()) o (glimpse()), la clase (class()), los nombres en las variables (names()), la presencia de faltantes (is.na())
dim(tab_1)
## [1] 120   7
str(tab_1)
## 'data.frame':    120 obs. of  7 variables:
##  $ Biomasa      : num  4.6 4.41 4.49 5.75 4.61 6.15 5.44 5.15 4.72 5.47 ...
##  $ Flores.r     : int  10 15 12 18 16 19 18 18 15 15 ...
##  $ Flores.d     : int  11 14 12 14 10 11 7 14 11 6 ...
##  $ Hojas.d      : int  69 51 24 291 91 34 210 240 34 130 ...
##  $ Plaga        : chr  "Presente" "Presente" "Ausente" "Ausente" ...
##  $ Estatus      : Factor w/ 3 levels "S","PA","MA": 1 1 1 1 1 1 1 1 1 1 ...
##  $ Fertilizacion: chr  "FI" "FO" "FI" "FI" ...
glimpse(tab_1)
## Rows: 120
## Columns: 7
## $ Biomasa       <dbl> 4.60, 4.41, 4.49, 5.75, 4.61, 6.15, 5.44, 5.15, 4.72, 5.~
## $ Flores.r      <int> 10, 15, 12, 18, 16, 19, 18, 18, 15, 15, 13, 17, 16, 16, ~
## $ Flores.d      <int> 11, 14, 12, 14, 10, 11, 7, 14, 11, 6, 12, 8, 5, 13, 13, ~
## $ Hojas.d       <int> 69, 51, 24, 291, 91, 34, 210, 240, 34, 130, 45, 8, 16, 2~
## $ Plaga         <chr> "Presente", "Presente", "Ausente", "Ausente", "Presente"~
## $ Estatus       <fct> S, S, S, S, S, S, S, S, S, S, S, S, S, S, S, S, S, S, S,~
## $ Fertilizacion <chr> "FI", "FO", "FI", "FI", "FI", "FI", "FI", "FO", "FI", "F~
class(tab_1)
## [1] "data.frame"
names(tab_1)
## [1] "Biomasa"       "Flores.r"      "Flores.d"      "Hojas.d"      
## [5] "Plaga"         "Estatus"       "Fertilizacion"
head(is.na(tab_1))
##      Biomasa Flores.r Flores.d Hojas.d Plaga Estatus Fertilizacion
## [1,]   FALSE    FALSE    FALSE   FALSE FALSE   FALSE         FALSE
## [2,]   FALSE    FALSE    FALSE   FALSE FALSE   FALSE         FALSE
## [3,]   FALSE    FALSE    FALSE   FALSE FALSE   FALSE         FALSE
## [4,]   FALSE    FALSE    FALSE   FALSE FALSE   FALSE         FALSE
## [5,]   FALSE    FALSE    FALSE   FALSE FALSE   FALSE         FALSE
## [6,]   FALSE    FALSE    FALSE   FALSE FALSE   FALSE         FALSE
  • Seleccione un subconjunto (el 75 % de las filas de todas las columnas) de todo el marco de datos o tableta. Use la función (sample n()) y asigne un valor faltante NA a dos variables cuantitativas cualesquiera del conjunto de datos muestreado.
tab_2 <- sample_n(tab_1, size = 90, replace = FALSE)
head(tab_2) 
##   Biomasa Flores.r Flores.d Hojas.d    Plaga Estatus Fertilizacion
## 1    5.40       16        7      59 Presente      PA            FI
## 2    4.24       17       13     173  Ausente       S            FO
## 3    4.54       18       11     289 Presente      PA            FI
## 4    6.18       15        6      93 Presente      PA            FI
## 5    4.15       17        6     144  Ausente      MA            FI
## 6    4.61       16       10      91 Presente       S            FI
tab_2$Biomasa[8] = NA
tab_2
##    Biomasa Flores.r Flores.d Hojas.d    Plaga Estatus Fertilizacion
## 1     5.40       16        7      59 Presente      PA            FI
## 2     4.24       17       13     173  Ausente       S            FO
## 3     4.54       18       11     289 Presente      PA            FI
## 4     6.18       15        6      93 Presente      PA            FI
## 5     4.15       17        6     144  Ausente      MA            FI
## 6     4.61       16       10      91 Presente       S            FI
## 7     5.47       16        9     207 Presente       S            FI
## 8       NA       16       13     217 Presente       S            FI
## 9     5.96       15        5      33  Ausente      PA            FO
## 10    4.56       14       10     138  Ausente      MA            FO
## 11    5.76       15       11      74  Ausente       S            FO
## 12    4.08       16       11     161 Presente      PA            FI
## 13    4.49       19        8     141 Presente      PA            FO
## 14    6.13       17       11     116  Ausente      PA            FO
## 15    4.75       16       16     135 Presente       S            FI
## 16    4.97       17        7      65 Presente      PA            FO
## 17    5.32       17        8      70 Presente       S            FO
## 18    4.25       18       13     216 Presente      PA            FI
## 19    3.97       17        8     107  Ausente      MA            FI
## 20    4.89       16        6      56 Presente      PA            FO
## 21    3.98       11        4     192  Ausente      PA            FI
## 22    5.92       18       13      32 Presente       S            FO
## 23    4.02       16       11     183 Presente       S            FI
## 24    4.89       17        9     193 Presente      MA            FI
## 25    5.98       13       15     193 Presente      MA            FO
## 26    4.80       20       12     188 Presente       S            FO
## 27    4.14       15       14     166 Presente      PA            FI
## 28    5.75       18       14     291  Ausente       S            FI
## 29    6.45       15       16     180 Presente       S            FI
## 30    5.20       16       10     299 Presente      MA            FO
## 31    6.15       19       11      34 Presente       S            FI
## 32    4.80       16       12     246 Presente      MA            FO
## 33    4.17       17       14     291 Presente       S            FO
## 34    4.77       13       10      47 Presente      PA            FI
## 35    3.92       14        9     295  Ausente      PA            FO
## 36    4.72       18        6      85  Ausente      MA            FO
## 37    5.57       13        9     210 Presente       S            FI
## 38    5.80       17        4     201 Presente      PA            FO
## 39    4.01       14       10     135 Presente      MA            FO
## 40    5.42       16        8     158 Presente      MA            FO
## 41    4.59       16        5      70 Presente      MA            FO
## 42    3.26       16        5      16 Presente       S            FO
## 43    4.61       20        9     220 Presente      PA            FO
## 44    5.38       17       12     210 Presente      PA            FI
## 45    5.57       13        2      90 Presente      MA            FI
## 46    6.23       17        9     142 Presente      MA            FO
## 47    5.99       18       14     164 Presente      PA            FO
## 48    4.25       18       13     275  Ausente      PA            FI
## 49    3.42       15        8     191 Presente      MA            FO
## 50    4.16       12       11      92 Presente      MA            FO
## 51    5.44       15       19     158 Presente      MA            FI
## 52    4.33       16       14      25 Presente       S            FI
## 53    6.33       15        4     118 Presente      MA            FO
## 54    4.73       15       10      90 Presente       S            FI
## 55    3.96       17        7     150 Presente      MA            FI
## 56    5.98       11       12     204 Presente      PA            FO
## 57    5.62       17       10      74  Ausente      MA            FO
## 58    5.03       18       11     150  Ausente       S            FI
## 59    3.98       20       10     294 Presente      PA            FI
## 60    4.08       14       14     220 Presente      PA            FO
## 61    6.11       15       11       7 Presente      MA            FO
## 62    4.64       13        7     121  Ausente      MA            FO
## 63    4.39       11        8     152 Presente       S            FO
## 64    6.00       17       10     245 Presente      MA            FI
## 65    4.39       14       12      96 Presente       S            FO
## 66    4.04       19       10      76 Presente       S            FI
## 67    4.36       17        8      12 Presente      MA            FI
## 68    5.37       18       11     113 Presente      MA            FO
## 69    5.52       15       13     248 Presente      MA            FI
## 70    3.78       14        8     236 Presente      PA            FI
## 71    4.66       17       13      29 Presente      MA            FO
## 72    3.40       17       13     198 Presente       S            FO
## 73    4.66       13       12      45 Presente       S            FI
## 74    5.50       16        5      64  Ausente      PA            FI
## 75    4.67       16       16     259 Presente      PA            FI
## 76    4.62       15        9     266  Ausente      PA            FI
## 77    4.48       19        9     135 Presente      MA            FI
## 78    3.64       16        8     248 Presente      PA            FI
## 79    5.57       17       16     165 Presente      PA            FI
## 80    4.72       15       11      34 Presente       S            FI
## 81    4.40       17        5     165 Presente      PA            FI
## 82    5.26       16        6      69 Presente      MA            FI
## 83    6.00       18        7     257 Presente      PA            FI
## 84    5.43       17        5      51  Ausente      PA            FI
## 85    5.47       15        6     130 Presente       S            FI
## 86    5.99       12        9     238  Ausente       S            FI
## 87    4.59       16       10      50 Presente      PA            FI
## 88    5.44       18        7     210 Presente       S            FI
## 89    5.41       18        8     164 Presente       S            FO
## 90    4.66       18        9     237 Presente      PA            FO
tab_2$Hojas.d[32] = NA
tab_2
##    Biomasa Flores.r Flores.d Hojas.d    Plaga Estatus Fertilizacion
## 1     5.40       16        7      59 Presente      PA            FI
## 2     4.24       17       13     173  Ausente       S            FO
## 3     4.54       18       11     289 Presente      PA            FI
## 4     6.18       15        6      93 Presente      PA            FI
## 5     4.15       17        6     144  Ausente      MA            FI
## 6     4.61       16       10      91 Presente       S            FI
## 7     5.47       16        9     207 Presente       S            FI
## 8       NA       16       13     217 Presente       S            FI
## 9     5.96       15        5      33  Ausente      PA            FO
## 10    4.56       14       10     138  Ausente      MA            FO
## 11    5.76       15       11      74  Ausente       S            FO
## 12    4.08       16       11     161 Presente      PA            FI
## 13    4.49       19        8     141 Presente      PA            FO
## 14    6.13       17       11     116  Ausente      PA            FO
## 15    4.75       16       16     135 Presente       S            FI
## 16    4.97       17        7      65 Presente      PA            FO
## 17    5.32       17        8      70 Presente       S            FO
## 18    4.25       18       13     216 Presente      PA            FI
## 19    3.97       17        8     107  Ausente      MA            FI
## 20    4.89       16        6      56 Presente      PA            FO
## 21    3.98       11        4     192  Ausente      PA            FI
## 22    5.92       18       13      32 Presente       S            FO
## 23    4.02       16       11     183 Presente       S            FI
## 24    4.89       17        9     193 Presente      MA            FI
## 25    5.98       13       15     193 Presente      MA            FO
## 26    4.80       20       12     188 Presente       S            FO
## 27    4.14       15       14     166 Presente      PA            FI
## 28    5.75       18       14     291  Ausente       S            FI
## 29    6.45       15       16     180 Presente       S            FI
## 30    5.20       16       10     299 Presente      MA            FO
## 31    6.15       19       11      34 Presente       S            FI
## 32    4.80       16       12      NA Presente      MA            FO
## 33    4.17       17       14     291 Presente       S            FO
## 34    4.77       13       10      47 Presente      PA            FI
## 35    3.92       14        9     295  Ausente      PA            FO
## 36    4.72       18        6      85  Ausente      MA            FO
## 37    5.57       13        9     210 Presente       S            FI
## 38    5.80       17        4     201 Presente      PA            FO
## 39    4.01       14       10     135 Presente      MA            FO
## 40    5.42       16        8     158 Presente      MA            FO
## 41    4.59       16        5      70 Presente      MA            FO
## 42    3.26       16        5      16 Presente       S            FO
## 43    4.61       20        9     220 Presente      PA            FO
## 44    5.38       17       12     210 Presente      PA            FI
## 45    5.57       13        2      90 Presente      MA            FI
## 46    6.23       17        9     142 Presente      MA            FO
## 47    5.99       18       14     164 Presente      PA            FO
## 48    4.25       18       13     275  Ausente      PA            FI
## 49    3.42       15        8     191 Presente      MA            FO
## 50    4.16       12       11      92 Presente      MA            FO
## 51    5.44       15       19     158 Presente      MA            FI
## 52    4.33       16       14      25 Presente       S            FI
## 53    6.33       15        4     118 Presente      MA            FO
## 54    4.73       15       10      90 Presente       S            FI
## 55    3.96       17        7     150 Presente      MA            FI
## 56    5.98       11       12     204 Presente      PA            FO
## 57    5.62       17       10      74  Ausente      MA            FO
## 58    5.03       18       11     150  Ausente       S            FI
## 59    3.98       20       10     294 Presente      PA            FI
## 60    4.08       14       14     220 Presente      PA            FO
## 61    6.11       15       11       7 Presente      MA            FO
## 62    4.64       13        7     121  Ausente      MA            FO
## 63    4.39       11        8     152 Presente       S            FO
## 64    6.00       17       10     245 Presente      MA            FI
## 65    4.39       14       12      96 Presente       S            FO
## 66    4.04       19       10      76 Presente       S            FI
## 67    4.36       17        8      12 Presente      MA            FI
## 68    5.37       18       11     113 Presente      MA            FO
## 69    5.52       15       13     248 Presente      MA            FI
## 70    3.78       14        8     236 Presente      PA            FI
## 71    4.66       17       13      29 Presente      MA            FO
## 72    3.40       17       13     198 Presente       S            FO
## 73    4.66       13       12      45 Presente       S            FI
## 74    5.50       16        5      64  Ausente      PA            FI
## 75    4.67       16       16     259 Presente      PA            FI
## 76    4.62       15        9     266  Ausente      PA            FI
## 77    4.48       19        9     135 Presente      MA            FI
## 78    3.64       16        8     248 Presente      PA            FI
## 79    5.57       17       16     165 Presente      PA            FI
## 80    4.72       15       11      34 Presente       S            FI
## 81    4.40       17        5     165 Presente      PA            FI
## 82    5.26       16        6      69 Presente      MA            FI
## 83    6.00       18        7     257 Presente      PA            FI
## 84    5.43       17        5      51  Ausente      PA            FI
## 85    5.47       15        6     130 Presente       S            FI
## 86    5.99       12        9     238  Ausente       S            FI
## 87    4.59       16       10      50 Presente      PA            FI
## 88    5.44       18        7     210 Presente       S            FI
## 89    5.41       18        8     164 Presente       S            FO
## 90    4.66       18        9     237 Presente      PA            FO

Ya tenemos dos conjuntos de datos, los que podemos guardar como tib.c al completo y tib.i al incompleto. Ahora si podemos usar algunas de las funciones de consideradas en clase.

tib.c<-tab_1
tib.i<-tab_2 

ACTIVIDAD 3

  • Seleccione una variable cualquiera con (select()) para tib.c
head(select(.data =tib.c, Biomasa))
##   Biomasa
## 1    4.60
## 2    4.41
## 3    4.49
## 4    5.75
## 5    4.61
## 6    6.15
  • Seleccione desde la tercera a la sexta variable con (select(:)) para tib.c
head(
  tib.c %>%
    select(3:6)
)
##   Flores.d Hojas.d    Plaga Estatus
## 1       11      69 Presente       S
## 2       14      51 Presente       S
## 3       12      24  Ausente       S
## 4       14     291  Ausente       S
## 5       10      91 Presente       S
## 6       11      34 Presente       S
  • No seleccione desde la tercera a la sexta variable con (select(!(:))) para tib.c
head(
  tib.c %>%
    select(!(3:6))
)
##   Biomasa Flores.r Fertilizacion
## 1    4.60       10            FI
## 2    4.41       15            FO
## 3    4.49       12            FI
## 4    5.75       18            FI
## 5    4.61       16            FI
## 6    6.15       19            FI
  • Seleccione las que no finalizan con .d usando (select(!ends with()))
head(select(.data =tib.c,!ends_with(".d")))
##   Biomasa Flores.r    Plaga Estatus Fertilizacion
## 1    4.60       10 Presente       S            FI
## 2    4.41       15 Presente       S            FO
## 3    4.49       12  Ausente       S            FI
## 4    5.75       18  Ausente       S            FI
## 5    4.61       16 Presente       S            FI
## 6    6.15       19 Presente       S            FI
  • Seleccione las que comienzan con Fl usando (select(starts with()))
head(select(.data = tib.c,starts_with("Fl")))
##   Flores.r Flores.d
## 1       10       11
## 2       15       14
## 3       12       12
## 4       18       14
## 5       16       10
## 6       19       11

-Seleccione las que comienzan con F y terminan con .d usando (select(starts with() & ends with()))

head(select(.data=tib.c, starts_with("F") & ends_with(".d")))
##   Flores.d
## 1       11
## 2       14
## 3       12
## 4       14
## 5       10
## 6       11
  • Seleccione una variable cualquiera con (select()) para tib.c y agrúpela por Estatus usando ( group by() )
var_estatus <- tib.c %>% 
               group_by(Estatus)%>% 
               select(Biomasa)
## Adding missing grouping variables: `Estatus`
head(var_estatus)
## # A tibble: 6 x 2
## # Groups:   Estatus [1]
##   Estatus Biomasa
##   <fct>     <dbl>
## 1 S          4.6 
## 2 S          4.41
## 3 S          4.49
## 4 S          5.75
## 5 S          4.61
## 6 S          6.15
  • Guarde el resultado previo en la variable var estatus y ordene de mayor a menor por la misma variable anterior usando (arrange(desc(),.by group=TRUE)) para tib.c
head(arrange(.data = tib.c,desc(var_estatus),.by_group = TRUE))
##   Biomasa Flores.r Flores.d Hojas.d    Plaga Estatus Fertilizacion
## 1    6.33       15        4     118 Presente      MA            FO
## 2    6.23       17        9     142 Presente      MA            FO
## 3    6.11       15       11       7 Presente      MA            FO
## 4    6.03       18        9      34  Ausente      MA            FI
## 5    6.00       17       10     245 Presente      MA            FI
## 6    5.98       13       15     193 Presente      MA            FO
  • Filtre los datos que comienzan con Flores para el estatus muy afectadas ( Filter(,) )
head(
  tib.c %>% 
  filter( Estatus == 'MA') %>%
  select(starts_with('Flores'))
)
##   Flores.r Flores.d
## 1       17       11
## 2       18        9
## 3       13        2
## 4       17        6
## 5       19        9
## 6       17       10
  • Filtre los datos para cuando la biomasa es superior a 5 gramos
tib.c %>% 
  filter( Biomasa > 5) 
##    Biomasa Flores.r Flores.d Hojas.d    Plaga Estatus Fertilizacion
## 1     5.75       18       14     291  Ausente       S            FI
## 2     6.15       19       11      34 Presente       S            FI
## 3     5.44       18        7     210 Presente       S            FI
## 4     5.15       18       14     240  Ausente       S            FO
## 5     5.47       15        6     130 Presente       S            FI
## 6     5.73       16       13     217 Presente       S            FI
## 7     6.26       16        6     201 Presente       S            FO
## 8     5.32       17        8      70 Presente       S            FO
## 9     6.45       15       16     180 Presente       S            FI
## 10    5.57       13        9     210 Presente       S            FI
## 11    5.76       15       11      74  Ausente       S            FO
## 12    5.76       16        6     233  Ausente       S            FO
## 13    5.41       18        8     164 Presente       S            FO
## 14    5.51       16       10     175 Presente       S            FI
## 15    5.03       18       11     150  Ausente       S            FI
## 16    5.47       16        9     207 Presente       S            FI
## 17    5.92       18       13      32 Presente       S            FO
## 18    5.99       12        9     238  Ausente       S            FI
## 19    6.13       17       11     116  Ausente      PA            FO
## 20    5.98       11       12     204 Presente      PA            FO
## 21    5.29       17        8     272  Ausente      PA            FI
## 22    6.00       18        7     257 Presente      PA            FI
## 23    5.43       17        5      51  Ausente      PA            FI
## 24    5.40       16        7      59 Presente      PA            FI
## 25    5.50       16        5      64  Ausente      PA            FI
## 26    5.99       18       14     164 Presente      PA            FO
## 27    5.96       15        5      33  Ausente      PA            FO
## 28    6.75       15       10     111  Ausente      PA            FI
## 29    5.38       17       12     210 Presente      PA            FI
## 30    6.18       15        6      93 Presente      PA            FI
## 31    5.57       17       16     165 Presente      PA            FI
## 32    5.80       17        4     201 Presente      PA            FO
## 33    6.03       18        9      34  Ausente      MA            FI
## 34    5.57       13        2      90 Presente      MA            FI
## 35    6.00       17       10     245 Presente      MA            FI
## 36    5.98       13       15     193 Presente      MA            FO
## 37    5.62       17       10      74  Ausente      MA            FO
## 38    6.33       15        4     118 Presente      MA            FO
## 39    5.44       15       19     158 Presente      MA            FI
## 40    5.52       15       13     248 Presente      MA            FI
## 41    6.11       15       11       7 Presente      MA            FO
## 42    5.42       16        8     158 Presente      MA            FO
## 43    5.20       16       10     299 Presente      MA            FO
## 44    5.25       17        7     104 Presente      MA            FO
## 45    5.77       15       10     239 Presente      MA            FI
## 46    5.26       16        6      69 Presente      MA            FI
## 47    5.16       16        6     162  Ausente      MA            FI
## 48    6.23       17        9     142 Presente      MA            FO
## 49    5.37       18       11     113 Presente      MA            FO
  • Filtre los datos para tener solo fertilización se tienen plantas parcialmente afectadas y fueron tratadas con fertilización orgánica
head(
  tib.c %>% 
  filter( Estatus == 'PA' & Fertilizacion == 'FO') %>%
  select(Fertilizacion, Estatus)
)
##   Fertilizacion Estatus
## 1            FO      PA
## 2            FO      PA
## 3            FO      PA
## 4            FO      PA
## 5            FO      PA
## 6            FO      PA
  • Filtre los datos para tener solo fertilización se tienen plantas parcialmente afectadas o fueron tratadas con fertilización inorgánica
head(
  tib.c %>% 
  filter( Estatus == 'PA' |  Fertilizacion == 'FI') %>%
  select(Fertilizacion)
)
##   Fertilizacion
## 1            FI
## 2            FI
## 3            FI
## 4            FI
## 5            FI
## 6            FI
  • Filtre los datos de Flores desprendidas por presencia o ausencia de plaga para cuando el número de flores desprendidas es mayor a su mediana
median_Flores.d <- median(tib.c$Flores.d)
median_Flores.d
## [1] 10
 conteo=0
filtro=c()
for (i in tib.c$Flores.d){
    if (i > median(tib.c$Flores.d)){
      conteo=conteo+1
      filtro[conteo]=i
    }
}
  • Filtre los datos de una de las variables con datos faltantes por presencia o ausencia de plaga para cuando el valor de la variable con faltante es mayor a su mediana. Compare los resultados en los dos conjuntos de datos (completo o muestreado). Si encuentra diferencias en la mediana use median(, na.rm = TRUE)
median(tib.i$Biomasa, na.rm = TRUE)
## [1] 4.75
median(tib.c$Biomasa)
## [1] 4.725
  conteo=0
filtro=c()
newmedian<-median(tib.i$Biomasa, na.rm = TRUE)
for (i in tib.i$Biomasa){
    if (is.na(i) == TRUE){
      next
    }else if (i > newmedian){
      conteo=conteo+1
      filtro[conteo]=i
    }
}
  • Seleccione dos variables cuantitativas y asígnelas a un vector con c(,) y nómbrelo con v1. Ponga en otro vector dos valores numéricos que sirvan de condición a cada variable, por ejemplo, el cuartil inferior para una y el cuartil superior para la otra y llámelo v2. Usando ahora pipes, llame el data.frame completo y filtre usando el operador punto para seleccionar aquellos datos que superan respectivamente cada variable el valor de la condicion. Use ( v1=c();v2=c(); tib.c % > % filter(.data[[v1[[1]]>v2[[1]],.data[[v1[[2]]>v2[[2]]))
v1<-c(tib.c$Biomasa,tib.c$Hojas.d)
head(v1)
## [1] 4.60 4.41 4.49 5.75 4.61 6.15
qle_1b<-quantile(x = tib.c$Biomasa,probs =0.25 )
qle_1b
##    25% 
## 4.2475
qle_1h<-quantile(x = tib.c$Hojas.d,probs =0.25 )
qle_1h
##   25% 
## 88.75
v2<-c(qle_1b,qle_1h)
v2
##     25%     25% 
##  4.2475 88.7500
tib.c %>%
 filter(.[v1[1]]>v2[1],.[v1[2]]>v2[2])
##    Biomasa Flores.r Flores.d Hojas.d    Plaga Estatus Fertilizacion
## 1     5.75       18       14     291  Ausente       S            FI
## 2     4.61       16       10      91 Presente       S            FI
## 3     5.44       18        7     210 Presente       S            FI
## 4     5.15       18       14     240  Ausente       S            FO
## 5     5.47       15        6     130 Presente       S            FI
## 6     5.73       16       13     217 Presente       S            FI
## 7     4.24       17       13     173  Ausente       S            FO
## 8     4.39       14       12      96 Presente       S            FO
## 9     4.17       17       14     291 Presente       S            FO
## 10    6.26       16        6     201 Presente       S            FO
## 11    4.55       16        7     150 Presente       S            FI
## 12    4.12       14       10     299 Presente       S            FI
## 13    4.80       20       12     188 Presente       S            FO
## 14    6.45       15       16     180 Presente       S            FI
## 15    4.39       11        8     152 Presente       S            FO
## 16    5.57       13        9     210 Presente       S            FI
## 17    4.73       15       10      90 Presente       S            FI
## 18    3.40       17       13     198 Presente       S            FO
## 19    5.76       16        6     233  Ausente       S            FO
## 20    5.41       18        8     164 Presente       S            FO
## 21    3.39       16       12     201  Ausente       S            FI
## 22    4.02       16       11     183 Presente       S            FI
## 23    5.51       16       10     175 Presente       S            FI
## 24    5.03       18       11     150  Ausente       S            FI
## 25    5.47       16        9     207 Presente       S            FI
## 26    4.75       16       16     135 Presente       S            FI
## 27    5.99       12        9     238  Ausente       S            FI
## 28    4.14       15       14     166 Presente      PA            FI
## 29    6.13       17       11     116  Ausente      PA            FO
## 30    5.98       11       12     204 Presente      PA            FO
## 31    4.54       18       11     289 Presente      PA            FI
## 32    5.29       17        8     272  Ausente      PA            FI
## 33    4.49       19        8     141 Presente      PA            FO
## 34    4.50       18       11     198 Presente      PA            FO
## 35    6.00       18        7     257 Presente      PA            FI
## 36    4.94       18        4      96 Presente      PA            FI
## 37    4.40       17        5     165 Presente      PA            FI
## 38    4.05       12       16     235  Ausente      PA            FO
## 39    2.89       15       12     183  Ausente      PA            FO
## 40    4.66       18        9     237 Presente      PA            FO
## 41    3.98       11        4     192  Ausente      PA            FI
## 42    4.61       20        9     220 Presente      PA            FO
## 43    3.64       16        8     248 Presente      PA            FI
## 44    5.99       18       14     164 Presente      PA            FO
## 45    3.82       16        7     227 Presente      PA            FO
## 46    3.98       20       10     294 Presente      PA            FI
## 47    6.75       15       10     111  Ausente      PA            FI
## 48    3.92       14        9     295  Ausente      PA            FO
## 49    4.25       18       13     216 Presente      PA            FI
## 50    4.25       18       13     275  Ausente      PA            FI
## 51    4.62       15        9     266  Ausente      PA            FI
## 52    5.38       17       12     210 Presente      PA            FI
## 53    4.08       16       11     161 Presente      PA            FI
## 54    6.18       15        6      93 Presente      PA            FI
## 55    5.57       17       16     165 Presente      PA            FI
## 56    4.08       14       14     220 Presente      PA            FO
## 57    3.78       14        8     236 Presente      PA            FI
## 58    4.67       16       16     259 Presente      PA            FI
## 59    5.80       17        4     201 Presente      PA            FO
## 60    5.57       13        2      90 Presente      MA            FI
## 61    4.15       17        6     144  Ausente      MA            FI
## 62    4.48       19        9     135 Presente      MA            FI
## 63    6.00       17       10     245 Presente      MA            FI
## 64    4.89       17        9     193 Presente      MA            FI
## 65    5.98       13       15     193 Presente      MA            FO
## 66    3.96       17        7     150 Presente      MA            FI
## 67    4.95       18       12     293 Presente      MA            FI
## 68    6.33       15        4     118 Presente      MA            FO
## 69    5.44       15       19     158 Presente      MA            FI
## 70    4.64       13        7     121  Ausente      MA            FO
## 71    5.52       15       13     248 Presente      MA            FI
## 72    4.80       16       12     246 Presente      MA            FO
## 73    5.42       16        8     158 Presente      MA            FO
## 74    5.20       16       10     299 Presente      MA            FO
## 75    4.01       14       10     135 Presente      MA            FO
## 76    4.47       18       13     115 Presente      MA            FO
## 77    5.25       17        7     104 Presente      MA            FO
## 78    4.56       14       10     138  Ausente      MA            FO
## 79    3.97       17        8     107  Ausente      MA            FI
## 80    5.77       15       10     239 Presente      MA            FI
## 81    4.65       17        7     126 Presente      MA            FO
## 82    3.17       18        6     281  Ausente      MA            FI
## 83    5.16       16        6     162  Ausente      MA            FI
## 84    3.96       18        8     282  Ausente      MA            FI
## 85    6.23       17        9     142 Presente      MA            FO
## 86    3.42       15        8     191 Presente      MA            FO
## 87    5.37       18       11     113 Presente      MA            FO
## 88    4.15       17        9     268 Presente      MA            FO
## 89    4.16       12       11      92 Presente      MA            FO
## 90    4.15       17       13     278 Presente      MA            FO
  • Cree un data frame o tableta con todas las variables cuantitativas continuas estanda- rizadas con el score z y las discretas con la estadarización minimax y llámelo tib.e. Use (mutate())
tib.n = tib.c %>%
  mutate(
    Biomasa_z = (Biomasa - mean(Biomasa)) / sd(Biomasa),
    Flores.r_MinMax = ((Flores.r- min(Flores.r))/(max(Flores.r)-min(Flores.r))),
    Flores.d_MinMax = ((Flores.d- min(Flores.d))/(max(Flores.d)-min(Flores.d))),
    Hojas.d_MinMax = ((Hojas.d- min(Hojas.d))/(max(Hojas.d)-min(Hojas.d)))
    )
head(tib.n)
##   Biomasa Flores.r Flores.d Hojas.d    Plaga Estatus Fertilizacion  Biomasa_z
## 1    4.60       10       11      69 Presente       S            FI -0.3238075
## 2    4.41       15       14      51 Presente       S            FO -0.5533012
## 3    4.49       12       12      24  Ausente       S            FI -0.4566723
## 4    5.75       18       14     291  Ausente       S            FI  1.0652332
## 5    4.61       16       10      91 Presente       S            FI -0.3117289
## 6    6.15       19       11      34 Presente       S            FI  1.5483778
##   Flores.r_MinMax Flores.d_MinMax Hojas.d_MinMax
## 1             0.0       0.5294118     0.21232877
## 2             0.5       0.7058824     0.15068493
## 3             0.2       0.5882353     0.05821918
## 4             0.8       0.7058824     0.97260274
## 5             0.6       0.4705882     0.28767123
## 6             0.9       0.5294118     0.09246575
tib.e <- tib.n%>%
  select(Biomasa_z,Flores.r_MinMax,Flores.d_MinMax,Hojas.d_MinMax)
head(round(tib.e,2))
##   Biomasa_z Flores.r_MinMax Flores.d_MinMax Hojas.d_MinMax
## 1     -0.32             0.0            0.53           0.21
## 2     -0.55             0.5            0.71           0.15
## 3     -0.46             0.2            0.59           0.06
## 4      1.07             0.8            0.71           0.97
## 5     -0.31             0.6            0.47           0.29
## 6      1.55             0.9            0.53           0.09

ACTIVIDAD 4

  • Cree una nueva variable para tib.c donde divida el número de flores en las ramas con el número de flores desprendidas.
tib.c = tib.c %>%
  mutate(cociente_flores = Flores.r / Flores.d)

head(tib.c)
##   Biomasa Flores.r Flores.d Hojas.d    Plaga Estatus Fertilizacion
## 1    4.60       10       11      69 Presente       S            FI
## 2    4.41       15       14      51 Presente       S            FO
## 3    4.49       12       12      24  Ausente       S            FI
## 4    5.75       18       14     291  Ausente       S            FI
## 5    4.61       16       10      91 Presente       S            FI
## 6    6.15       19       11      34 Presente       S            FI
##   cociente_flores
## 1       0.9090909
## 2       1.0714286
## 3       1.0000000
## 4       1.2857143
## 5       1.6000000
## 6       1.7272727
  • Seleccione solo la variable del cociente previo agrupada por plaga. Cree una nueva variable que imprima el rango mínimo en cada grupo. LLame a la variable rangomin. Use (mutate( rangomin= min_rank(desc())))
tib.c %>%
  group_by(Plaga) %>%
  mutate( rangomin = min_rank(cociente_flores))  %>%
  select(cociente_flores,rangomin) %>%
  arrange(desc(rangomin))
## Adding missing grouping variables: `Plaga`
## # A tibble: 120 x 3
## # Groups:   Plaga [2]
##    Plaga    cociente_flores rangomin
##    <chr>              <dbl>    <int>
##  1 Presente            6.5        88
##  2 Presente            4.5        87
##  3 Presente            4.25       86
##  4 Presente            3.75       85
##  5 Presente            3.4        84
##  6 Presente            3.2        82
##  7 Presente            3.2        82
##  8 Presente            2.67       79
##  9 Presente            2.67       79
## 10 Presente            2.67       79
## # ... with 110 more rows
  • Renombre las variables asociadas a las flores a su gusto. Use (rename(tib.e,))
head(rename(tib.e,Flrs.r_MinMax=Flores.r_MinMax,Flrs.d_MinMax=Flores.d_MinMax))
##    Biomasa_z Flrs.r_MinMax Flrs.d_MinMax Hojas.d_MinMax
## 1 -0.3238075           0.0     0.5294118     0.21232877
## 2 -0.5533012           0.5     0.7058824     0.15068493
## 3 -0.4566723           0.2     0.5882353     0.05821918
## 4  1.0652332           0.8     0.7058824     0.97260274
## 5 -0.3117289           0.6     0.4705882     0.28767123
## 6  1.5483778           0.9     0.5294118     0.09246575
  • Pase a mayúsculas todos los nombres de las variables en cualquier tibble. Use (rename_with(tib.e,toupper))
head(rename_with(tib.e,toupper))
##    BIOMASA_Z FLORES.R_MINMAX FLORES.D_MINMAX HOJAS.D_MINMAX
## 1 -0.3238075             0.0       0.5294118     0.21232877
## 2 -0.5533012             0.5       0.7058824     0.15068493
## 3 -0.4566723             0.2       0.5882353     0.05821918
## 4  1.0652332             0.8       0.7058824     0.97260274
## 5 -0.3117289             0.6       0.4705882     0.28767123
## 6  1.5483778             0.9       0.5294118     0.09246575
  • Pase a minúsculas todos los nombres de variable y aproveche y cambie los .d por guiones al piso, es decir, d. Use (rename_with(tib.e,∼tolower(gsub(”.”,””,.x,fixed=TRUE))))
head(rename_with(tib.e,~ tolower(gsub(".", "_", .x, fixed = TRUE))))
##    biomasa_z flores_r_minmax flores_d_minmax hojas_d_minmax
## 1 -0.3238075             0.0       0.5294118     0.21232877
## 2 -0.5533012             0.5       0.7058824     0.15068493
## 3 -0.4566723             0.2       0.5882353     0.05821918
## 4  1.0652332             0.8       0.7058824     0.97260274
## 5 -0.3117289             0.6       0.4705882     0.28767123
## 6  1.5483778             0.9       0.5294118     0.09246575

ACTIVIDAD 5

  • Seleccione la variable biomasa de la tableta con faltantes y con summarise() obtenga la media y el número de datos de esta variable.
tib.i %>%
  select(Biomasa)%>%
  summarize(media_Biomasa = mean(Biomasa,na.rm = TRUE),n_Biomasa = n())
##   media_Biomasa n_Biomasa
## 1      4.924607        90
  • Seleccione la variable biomasa de la tableta con faltantes y con summarise() obtenga la media y el número de datos por tipo de fertilización.
tib.i %>%
  group_by(Fertilizacion) %>%
  summarize(media_Biomasa = round(mean(Biomasa, na.rm =TRUE),2),
            n_Biomasa = n())
## # A tibble: 2 x 3
##   Fertilizacion media_Biomasa n_Biomasa
##   <chr>                 <dbl>     <int>
## 1 FI                     4.9         50
## 2 FO                     4.96        40
  • Seleccione la variable biomasa de la tableta con faltantes y con summarise() obtenga los cuantiles 0.10,0.20,0.30,0.40 y 0.50 por tipo de fertilización.
tib.i %>%
  group_by(Fertilizacion) %>%
  summarize(q1=quantile(Biomasa,prob=0.10,na.rm =TRUE),
            q2=quantile(Biomasa,prob=0.20,na.rm =TRUE),
            q3=quantile(Biomasa,prob=0.30,na.rm =TRUE),
            q4=quantile(Biomasa,prob=0.40,na.rm =TRUE),
            q5=quantile(Biomasa,prob=0.50,na.rm =TRUE))
## # A tibble: 2 x 6
##   Fertilizacion    q1    q2    q3    q4    q5
##   <chr>         <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 FI             3.98  4.15  4.38  4.61  4.73
## 2 FO             4.00  4.23  4.54  4.65  4.8

-Seleccione la variable biomasa de la tableta con faltantes y con summarise() obtenga la media, mediana, máximo, mínimo, desviación típica, desviación media, media truncada y varianza por tipo de fertilización y plaga.

tib.i %>%
  group_by(Fertilizacion, Plaga) %>%
  summarize(media_Bio=mean(Biomasa,na.rm =TRUE),
            mediana_Bio=median(Biomasa,na.rm =TRUE),
            max_Bio=max(Biomasa,na.rm =TRUE),
            min_Bio=min(Biomasa,na.rm =TRUE),
            sd_Bio=sd(Biomasa,na.rm =TRUE),
            sd_media=mean(abs(Biomasa-mean(Biomasa,na.rm =TRUE))),
            mediatrunc_Bio=mean(Biomasa, trim = 0.1,na.rm =TRUE),
            var_Bio=var(Biomasa,na.rm =TRUE)
  )
## `summarise()` has grouped output by 'Fertilizacion'. You can override using the `.groups` argument.
## # A tibble: 4 x 10
## # Groups:   Fertilizacion [2]
##   Fertilizacion Plaga    media_Bio mediana_Bio max_Bio min_Bio sd_Bio sd_media
##   <chr>         <chr>        <dbl>       <dbl>   <dbl>   <dbl>  <dbl>    <dbl>
## 1 FI            Ausente       4.87        4.82    5.99    3.97  0.770    0.673
## 2 FI            Presente      4.90        4.73    6.45    3.64  0.742   NA    
## 3 FO            Ausente       5.06        4.72    6.13    3.92  0.812    0.717
## 4 FO            Presente      4.93        4.8     6.33    3.26  0.863    0.711
## # ... with 2 more variables: mediatrunc_Bio <dbl>, var_Bio <dbl>

-Seleccione la variable biomasa de la tableta con faltantes y con summarise() obtenga la media, mediana, máximo, mínimo, desviación típica, desviación media, media truncada y varianza por tipo de fertilización y plaga filtrando por plantas sanas.

tib.i %>%
  filter(Estatus=='S')%>%
  group_by(Fertilizacion, Plaga,Estatus) %>%
  summarize(media_Bio=mean(Biomasa,na.rm =TRUE),
            mediana_Bio=median(Biomasa,na.rm =TRUE),
            max_Bio=max(Biomasa,na.rm =TRUE),
            min_Bio=min(Biomasa,na.rm =TRUE),
            sd_Bio=sd(Biomasa,na.rm =TRUE),
            sd_media=mean(abs(Biomasa-mean(Biomasa,na.rm =TRUE))),
            mediatrunc_Bio=mean(Biomasa, trim = 0.1,na.rm =TRUE),
            var_Bio=var(Biomasa,na.rm =TRUE)
  )
## `summarise()` has grouped output by 'Fertilizacion', 'Plaga'. You can override using the `.groups` argument.
## # A tibble: 4 x 11
## # Groups:   Fertilizacion, Plaga [4]
##   Fertilizacion Plaga    Estatus media_Bio mediana_Bio max_Bio min_Bio sd_Bio
##   <chr>         <chr>    <fct>       <dbl>       <dbl>   <dbl>   <dbl>  <dbl>
## 1 FI            Ausente  S            5.59        5.75    5.99    5.03  0.500
## 2 FI            Presente S            5.03        4.74    6.45    4.02  0.743
## 3 FO            Ausente  S            5           5       5.76    4.24  1.07 
## 4 FO            Presente S            4.56        4.39    5.92    3.26  0.898
## # ... with 3 more variables: sd_media <dbl>, mediatrunc_Bio <dbl>,
## #   var_Bio <dbl>
  • Con la tableta con faltantes use la función drop_na() para sacar los faltantes y compare las estadísticas obtenidas en el item anterior con y sin faltantes
tib.i %>%
  drop_na()%>%
  filter(Estatus=='S')%>%
  group_by(Fertilizacion, Plaga,Estatus) %>%
  summarize(media_Bio=mean(Biomasa),
            mediana_Bio=median(Biomasa),
            max_Bio=max(Biomasa),
            min_Bio=min(Biomasa),
            sd_Bio=sd(Biomasa),
            mediatrunc_Bio=mean(Biomasa, trim = 0.1),
            var_Bio=var(Biomasa)
  )
## `summarise()` has grouped output by 'Fertilizacion', 'Plaga'. You can override using the `.groups` argument.
## # A tibble: 4 x 10
## # Groups:   Fertilizacion, Plaga [4]
##   Fertilizacion Plaga    Estatus media_Bio mediana_Bio max_Bio min_Bio sd_Bio
##   <chr>         <chr>    <fct>       <dbl>       <dbl>   <dbl>   <dbl>  <dbl>
## 1 FI            Ausente  S            5.59        5.75    5.99    5.03  0.500
## 2 FI            Presente S            5.03        4.74    6.45    4.02  0.743
## 3 FO            Ausente  S            5           5       5.76    4.24  1.07 
## 4 FO            Presente S            4.56        4.39    5.92    3.26  0.898
## # ... with 2 more variables: mediatrunc_Bio <dbl>, var_Bio <dbl>
tib.dro <- drop_na(tib.i)
head(tib.dro)
##   Biomasa Flores.r Flores.d Hojas.d    Plaga Estatus Fertilizacion
## 1    5.40       16        7      59 Presente      PA            FI
## 2    4.24       17       13     173  Ausente       S            FO
## 3    4.54       18       11     289 Presente      PA            FI
## 4    6.18       15        6      93 Presente      PA            FI
## 5    4.15       17        6     144  Ausente      MA            FI
## 6    4.61       16       10      91 Presente       S            FI
tib.dro %>%
  filter(Estatus=='S')%>%
  group_by(Fertilizacion, Plaga,Estatus) %>%
  summarize(media_Bio=mean(Biomasa),
            mediana_Bio=median(Biomasa),
            max_Bio=max(Biomasa),
            min_Bio=min(Biomasa),
            sd_Bio=sd(Biomasa),
            mediatrunc_Bio=mean(Biomasa, trim = 0.1),
            var_Bio=var(Biomasa)
  )
## `summarise()` has grouped output by 'Fertilizacion', 'Plaga'. You can override using the `.groups` argument.
## # A tibble: 4 x 10
## # Groups:   Fertilizacion, Plaga [4]
##   Fertilizacion Plaga    Estatus media_Bio mediana_Bio max_Bio min_Bio sd_Bio
##   <chr>         <chr>    <fct>       <dbl>       <dbl>   <dbl>   <dbl>  <dbl>
## 1 FI            Ausente  S            5.59        5.75    5.99    5.03  0.500
## 2 FI            Presente S            5.03        4.74    6.45    4.02  0.743
## 3 FO            Ausente  S            5           5       5.76    4.24  1.07 
## 4 FO            Presente S            4.56        4.39    5.92    3.26  0.898
## # ... with 2 more variables: mediatrunc_Bio <dbl>, var_Bio <dbl>

NOTA: Las estadísticas no cambian. En el ítem anterior se aplicó na.rm para omitir los NA y en este ítem se uso la función drop_na() para sacar los faltantes.

  • Filtre los datos seleccionando solo las plantas afectadas o muy afectadas.Use el operador %in %
target <- c("PA","MA") 
filter(tib.i,Estatus %in% target)
##    Biomasa Flores.r Flores.d Hojas.d    Plaga Estatus Fertilizacion
## 1     5.40       16        7      59 Presente      PA            FI
## 2     4.54       18       11     289 Presente      PA            FI
## 3     6.18       15        6      93 Presente      PA            FI
## 4     4.15       17        6     144  Ausente      MA            FI
## 5     5.96       15        5      33  Ausente      PA            FO
## 6     4.56       14       10     138  Ausente      MA            FO
## 7     4.08       16       11     161 Presente      PA            FI
## 8     4.49       19        8     141 Presente      PA            FO
## 9     6.13       17       11     116  Ausente      PA            FO
## 10    4.97       17        7      65 Presente      PA            FO
## 11    4.25       18       13     216 Presente      PA            FI
## 12    3.97       17        8     107  Ausente      MA            FI
## 13    4.89       16        6      56 Presente      PA            FO
## 14    3.98       11        4     192  Ausente      PA            FI
## 15    4.89       17        9     193 Presente      MA            FI
## 16    5.98       13       15     193 Presente      MA            FO
## 17    4.14       15       14     166 Presente      PA            FI
## 18    5.20       16       10     299 Presente      MA            FO
## 19    4.80       16       12      NA Presente      MA            FO
## 20    4.77       13       10      47 Presente      PA            FI
## 21    3.92       14        9     295  Ausente      PA            FO
## 22    4.72       18        6      85  Ausente      MA            FO
## 23    5.80       17        4     201 Presente      PA            FO
## 24    4.01       14       10     135 Presente      MA            FO
## 25    5.42       16        8     158 Presente      MA            FO
## 26    4.59       16        5      70 Presente      MA            FO
## 27    4.61       20        9     220 Presente      PA            FO
## 28    5.38       17       12     210 Presente      PA            FI
## 29    5.57       13        2      90 Presente      MA            FI
## 30    6.23       17        9     142 Presente      MA            FO
## 31    5.99       18       14     164 Presente      PA            FO
## 32    4.25       18       13     275  Ausente      PA            FI
## 33    3.42       15        8     191 Presente      MA            FO
## 34    4.16       12       11      92 Presente      MA            FO
## 35    5.44       15       19     158 Presente      MA            FI
## 36    6.33       15        4     118 Presente      MA            FO
## 37    3.96       17        7     150 Presente      MA            FI
## 38    5.98       11       12     204 Presente      PA            FO
## 39    5.62       17       10      74  Ausente      MA            FO
## 40    3.98       20       10     294 Presente      PA            FI
## 41    4.08       14       14     220 Presente      PA            FO
## 42    6.11       15       11       7 Presente      MA            FO
## 43    4.64       13        7     121  Ausente      MA            FO
## 44    6.00       17       10     245 Presente      MA            FI
## 45    4.36       17        8      12 Presente      MA            FI
## 46    5.37       18       11     113 Presente      MA            FO
## 47    5.52       15       13     248 Presente      MA            FI
## 48    3.78       14        8     236 Presente      PA            FI
## 49    4.66       17       13      29 Presente      MA            FO
## 50    5.50       16        5      64  Ausente      PA            FI
## 51    4.67       16       16     259 Presente      PA            FI
## 52    4.62       15        9     266  Ausente      PA            FI
## 53    4.48       19        9     135 Presente      MA            FI
## 54    3.64       16        8     248 Presente      PA            FI
## 55    5.57       17       16     165 Presente      PA            FI
## 56    4.40       17        5     165 Presente      PA            FI
## 57    5.26       16        6      69 Presente      MA            FI
## 58    6.00       18        7     257 Presente      PA            FI
## 59    5.43       17        5      51  Ausente      PA            FI
## 60    4.59       16       10      50 Presente      PA            FI
## 61    4.66       18        9     237 Presente      PA            FO
  • Seleccione la tibble con faltantes y use complete.cases(.) para dejar por fuera los faltantes
tib.h <- tib.i[complete.cases(tib.i), ] 
tib.h
##    Biomasa Flores.r Flores.d Hojas.d    Plaga Estatus Fertilizacion
## 1     5.40       16        7      59 Presente      PA            FI
## 2     4.24       17       13     173  Ausente       S            FO
## 3     4.54       18       11     289 Presente      PA            FI
## 4     6.18       15        6      93 Presente      PA            FI
## 5     4.15       17        6     144  Ausente      MA            FI
## 6     4.61       16       10      91 Presente       S            FI
## 7     5.47       16        9     207 Presente       S            FI
## 9     5.96       15        5      33  Ausente      PA            FO
## 10    4.56       14       10     138  Ausente      MA            FO
## 11    5.76       15       11      74  Ausente       S            FO
## 12    4.08       16       11     161 Presente      PA            FI
## 13    4.49       19        8     141 Presente      PA            FO
## 14    6.13       17       11     116  Ausente      PA            FO
## 15    4.75       16       16     135 Presente       S            FI
## 16    4.97       17        7      65 Presente      PA            FO
## 17    5.32       17        8      70 Presente       S            FO
## 18    4.25       18       13     216 Presente      PA            FI
## 19    3.97       17        8     107  Ausente      MA            FI
## 20    4.89       16        6      56 Presente      PA            FO
## 21    3.98       11        4     192  Ausente      PA            FI
## 22    5.92       18       13      32 Presente       S            FO
## 23    4.02       16       11     183 Presente       S            FI
## 24    4.89       17        9     193 Presente      MA            FI
## 25    5.98       13       15     193 Presente      MA            FO
## 26    4.80       20       12     188 Presente       S            FO
## 27    4.14       15       14     166 Presente      PA            FI
## 28    5.75       18       14     291  Ausente       S            FI
## 29    6.45       15       16     180 Presente       S            FI
## 30    5.20       16       10     299 Presente      MA            FO
## 31    6.15       19       11      34 Presente       S            FI
## 33    4.17       17       14     291 Presente       S            FO
## 34    4.77       13       10      47 Presente      PA            FI
## 35    3.92       14        9     295  Ausente      PA            FO
## 36    4.72       18        6      85  Ausente      MA            FO
## 37    5.57       13        9     210 Presente       S            FI
## 38    5.80       17        4     201 Presente      PA            FO
## 39    4.01       14       10     135 Presente      MA            FO
## 40    5.42       16        8     158 Presente      MA            FO
## 41    4.59       16        5      70 Presente      MA            FO
## 42    3.26       16        5      16 Presente       S            FO
## 43    4.61       20        9     220 Presente      PA            FO
## 44    5.38       17       12     210 Presente      PA            FI
## 45    5.57       13        2      90 Presente      MA            FI
## 46    6.23       17        9     142 Presente      MA            FO
## 47    5.99       18       14     164 Presente      PA            FO
## 48    4.25       18       13     275  Ausente      PA            FI
## 49    3.42       15        8     191 Presente      MA            FO
## 50    4.16       12       11      92 Presente      MA            FO
## 51    5.44       15       19     158 Presente      MA            FI
## 52    4.33       16       14      25 Presente       S            FI
## 53    6.33       15        4     118 Presente      MA            FO
## 54    4.73       15       10      90 Presente       S            FI
## 55    3.96       17        7     150 Presente      MA            FI
## 56    5.98       11       12     204 Presente      PA            FO
## 57    5.62       17       10      74  Ausente      MA            FO
## 58    5.03       18       11     150  Ausente       S            FI
## 59    3.98       20       10     294 Presente      PA            FI
## 60    4.08       14       14     220 Presente      PA            FO
## 61    6.11       15       11       7 Presente      MA            FO
## 62    4.64       13        7     121  Ausente      MA            FO
## 63    4.39       11        8     152 Presente       S            FO
## 64    6.00       17       10     245 Presente      MA            FI
## 65    4.39       14       12      96 Presente       S            FO
## 66    4.04       19       10      76 Presente       S            FI
## 67    4.36       17        8      12 Presente      MA            FI
## 68    5.37       18       11     113 Presente      MA            FO
## 69    5.52       15       13     248 Presente      MA            FI
## 70    3.78       14        8     236 Presente      PA            FI
## 71    4.66       17       13      29 Presente      MA            FO
## 72    3.40       17       13     198 Presente       S            FO
## 73    4.66       13       12      45 Presente       S            FI
## 74    5.50       16        5      64  Ausente      PA            FI
## 75    4.67       16       16     259 Presente      PA            FI
## 76    4.62       15        9     266  Ausente      PA            FI
## 77    4.48       19        9     135 Presente      MA            FI
## 78    3.64       16        8     248 Presente      PA            FI
## 79    5.57       17       16     165 Presente      PA            FI
## 80    4.72       15       11      34 Presente       S            FI
## 81    4.40       17        5     165 Presente      PA            FI
## 82    5.26       16        6      69 Presente      MA            FI
## 83    6.00       18        7     257 Presente      PA            FI
## 84    5.43       17        5      51  Ausente      PA            FI
## 85    5.47       15        6     130 Presente       S            FI
## 86    5.99       12        9     238  Ausente       S            FI
## 87    4.59       16       10      50 Presente      PA            FI
## 88    5.44       18        7     210 Presente       S            FI
## 89    5.41       18        8     164 Presente       S            FO
## 90    4.66       18        9     237 Presente      PA            FO
  • Elimine de cualquier tibble las columnas asociadas al conteo de flores
head(
  tib.i %>%
  select(-Flores.r,-Flores.d)
)
##   Biomasa Hojas.d    Plaga Estatus Fertilizacion
## 1    5.40      59 Presente      PA            FI
## 2    4.24     173  Ausente       S            FO
## 3    4.54     289 Presente      PA            FI
## 4    6.18      93 Presente      PA            FI
## 5    4.15     144  Ausente      MA            FI
## 6    4.61      91 Presente       S            FI
  • Seleccione de cualquier tibble las avriables que contengan la d. Use select(contains())
head(
  tib.i %>%
  select(contains('.d'))
)
##   Flores.d Hojas.d
## 1        7      59
## 2       13     173
## 3       11     289
## 4        6      93
## 5        6     144
## 6       10      91
  • Reordene una tableta usando select(,everything()) colocando primero los conteos de flores
head(
  tib.i %>%
  select(Flores.r,Flores.d,everything()) 
)
##   Flores.r Flores.d Biomasa Hojas.d    Plaga Estatus Fertilizacion
## 1       16        7    5.40      59 Presente      PA            FI
## 2       17       13    4.24     173  Ausente       S            FO
## 3       18       11    4.54     289 Presente      PA            FI
## 4       15        6    6.18      93 Presente      PA            FI
## 5       17        6    4.15     144  Ausente      MA            FI
## 6       16       10    4.61      91 Presente       S            FI

```