#Actividad 1
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.1.1
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(purrr)
## Warning: package 'purrr' was built under R version 4.1.1
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.1.1
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v readr   2.0.2
## v tibble  3.1.5     v stringr 1.4.0
## v tidyr   1.1.4     v forcats 0.5.1
## Warning: package 'ggplot2' was built under R version 4.1.1
## Warning: package 'tibble' was built under R version 4.1.1
## Warning: package 'tidyr' was built under R version 4.1.1
## Warning: package 'readr' was built under R version 4.1.1
## Warning: package 'forcats' was built under R version 4.1.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(tidyr)
library(DT)
## Warning: package 'DT' was built under R version 4.1.1
library(data.table)
## Warning: package 'data.table' was built under R version 4.1.1
## 
## Attaching package: 'data.table'
## The following object is masked from 'package:purrr':
## 
##     transpose
## The following objects are masked from 'package:dplyr':
## 
##     between, first, last

Actividad 1

set.seed(2071)


d_normal = rnorm(n= 120 , mean = 5, sd = 0.85 )

d_binomial = rbinom(size = 120 , n = 20 , prob = 0.8)

d_poisson = rpois(120, lambda = 10.5) 

Muestra = sample.int(300, size = 120, replace =  F )
library(purrr)
bernoulli = rbernoulli(p = 0.75,n =120)
ifelse(bernoulli ==TRUE, "presente","ausente")
##   [1] "presente" "presente" "ausente"  "presente" "ausente"  "presente"
##   [7] "presente" "presente" "presente" "presente" "presente" "presente"
##  [13] "ausente"  "presente" "ausente"  "ausente"  "presente" "presente"
##  [19] "presente" "presente" "presente" "presente" "presente" "presente"
##  [25] "presente" "presente" "presente" "presente" "ausente"  "presente"
##  [31] "presente" "presente" "ausente"  "presente" "presente" "presente"
##  [37] "ausente"  "presente" "ausente"  "presente" "presente" "presente"
##  [43] "presente" "presente" "ausente"  "ausente"  "presente" "presente"
##  [49] "ausente"  "presente" "presente" "ausente"  "presente" "presente"
##  [55] "presente" "presente" "presente" "ausente"  "presente" "ausente" 
##  [61] "presente" "presente" "ausente"  "presente" "presente" "ausente" 
##  [67] "presente" "presente" "presente" "presente" "ausente"  "presente"
##  [73] "presente" "presente" "presente" "presente" "ausente"  "presente"
##  [79] "presente" "presente" "presente" "presente" "presente" "presente"
##  [85] "presente" "presente" "ausente"  "presente" "ausente"  "presente"
##  [91] "presente" "presente" "ausente"  "presente" "ausente"  "presente"
##  [97] "presente" "ausente"  "presente" "presente" "ausente"  "ausente" 
## [103] "presente" "presente" "presente" "presente" "presente" "ausente" 
## [109] "presente" "presente" "presente" "ausente"  "presente" "presente"
## [115] "presente" "presente" "presente" "ausente"  "ausente"  "ausente"
data.table(bernoulli)
##      bernoulli
##   1:      TRUE
##   2:      TRUE
##   3:     FALSE
##   4:      TRUE
##   5:     FALSE
##  ---          
## 116:      TRUE
## 117:      TRUE
## 118:     FALSE
## 119:     FALSE
## 120:     FALSE
name_bernoulli <- ifelse(bernoulli ==TRUE, "presente","ausente")
data.table(name_bernoulli)
##      name_bernoulli
##   1:       presente
##   2:       presente
##   3:        ausente
##   4:       presente
##   5:        ausente
##  ---               
## 116:       presente
## 117:       presente
## 118:        ausente
## 119:        ausente
## 120:        ausente
afectación = gl(n = 3, k = 40, length = 120, labels = c("S" , "PA" , "MA"))

## S:Planta sana; PA:planta parcialmente afectada; MA:Muy afectadas 
d_unifrome <- runif(n = 120, min = 0, max = 1.2)
fertilizacion = ifelse(d_unifrome < 0.5, "FO" , "FI")
## FO:fertilización organica; FI: fertilización inorganica 

Actividad 2

##data frame (tib.c)
tib.c = data.frame(Biomasa = d_normal, Flores.r = d_binomial, Flores.d = d_poisson, Hojas.d = Muestra, plaga = name_bernoulli, Estatus = afectación, Fertilizacion = fertilizacion)
data.table(tib.c)
##       Biomasa Flores.r Flores.d Hojas.d    plaga Estatus Fertilizacion
##   1: 5.346747      101        9     278 presente       S            FI
##   2: 5.100974       98       18      81 presente       S            FI
##   3: 4.019105       95       13      64  ausente       S            FI
##   4: 5.937088       97        9     200 presente       S            FO
##   5: 6.011233       98        9      56  ausente       S            FO
##  ---                                                                  
## 116: 6.153520       81        9     220 presente      MA            FI
## 117: 4.289028       97       11     139 presente      MA            FO
## 118: 5.256562       98       14      32  ausente      MA            FI
## 119: 6.384870       97       13     214  ausente      MA            FO
## 120: 5.721456       94       12     273  ausente      MA            FO
##dimension data frame 
dim(tib.c)
## [1] 120   7
##estructura data frame 
str(tib.c)
## 'data.frame':    120 obs. of  7 variables:
##  $ Biomasa      : num  5.35 5.1 4.02 5.94 6.01 ...
##  $ Flores.r     : int  101 98 95 97 98 97 88 95 96 92 ...
##  $ Flores.d     : int  9 18 13 9 9 8 14 11 10 15 ...
##  $ Hojas.d      : int  278 81 64 200 56 188 65 292 66 212 ...
##  $ plaga        : chr  "presente" "presente" "ausente" "presente" ...
##  $ Estatus      : Factor w/ 3 levels "S","PA","MA": 1 1 1 1 1 1 1 1 1 1 ...
##  $ Fertilizacion: chr  "FI" "FI" "FI" "FO" ...
##clase data frame
class(tib.c)
## [1] "data.frame"
##nombres variables
names(tib.c)
## [1] "Biomasa"       "Flores.r"      "Flores.d"      "Hojas.d"      
## [5] "plaga"         "Estatus"       "Fertilizacion"
##presencia de faltantes
Faltantes <- is.na(tib.c) 
data.table(Faltantes)
##      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
##  ---                                                              
## 116:   FALSE    FALSE    FALSE   FALSE FALSE   FALSE         FALSE
## 117:   FALSE    FALSE    FALSE   FALSE FALSE   FALSE         FALSE
## 118:   FALSE    FALSE    FALSE   FALSE FALSE   FALSE         FALSE
## 119:   FALSE    FALSE    FALSE   FALSE FALSE   FALSE         FALSE
## 120:   FALSE    FALSE    FALSE   FALSE FALSE   FALSE         FALSE
#asignar valores (faltantes)
 tib.i = sample_n(tib.c, 90, replace = FALSE)
tib.i$Hojas.d[c(8,15)] = NA
data.table(head(tib.i))
##     Biomasa Flores.r Flores.d Hojas.d    plaga Estatus Fertilizacion
## 1: 5.937088       97        9     200 presente       S            FO
## 2: 2.835715       94       11      94 presente      MA            FI
## 3: 5.654898       95       17     129 presente      MA            FI
## 4: 5.913560       97        9      91 presente       S            FI
## 5: 5.482667       97        8     241 presente       S            FI
## 6: 4.151177       88       13     141 presente      MA            FI

Actividad 3

library(dplyr)
##Seleccione una variable cualquiera con (select()) para tib.c
solo_bio = tib.c %>% select(Biomasa)
data.table(solo_bio)
##       Biomasa
##   1: 5.346747
##   2: 5.100974
##   3: 4.019105
##   4: 5.937088
##   5: 6.011233
##  ---         
## 116: 6.153520
## 117: 4.289028
## 118: 5.256562
## 119: 6.384870
## 120: 5.721456
##Seleccione desde la tercera a la sexta variable con (select(:)) para tib.c
selec = tib.c %>% select(3:6)
data.table(selec)
##      Flores.d Hojas.d    plaga Estatus
##   1:        9     278 presente       S
##   2:       18      81 presente       S
##   3:       13      64  ausente       S
##   4:        9     200 presente       S
##   5:        9      56  ausente       S
##  ---                                  
## 116:        9     220 presente      MA
## 117:       11     139 presente      MA
## 118:       14      32  ausente      MA
## 119:       13     214  ausente      MA
## 120:       12     273  ausente      MA
##No seleccione desde la tercera a la sexta variable con (select(!(:))) para tib.c
no_select = tib.c %>% select(!(3:6))
data.table(no_select)
##       Biomasa Flores.r Fertilizacion
##   1: 5.346747      101            FI
##   2: 5.100974       98            FI
##   3: 4.019105       95            FI
##   4: 5.937088       97            FO
##   5: 6.011233       98            FO
##  ---                                
## 116: 6.153520       81            FI
## 117: 4.289028       97            FO
## 118: 5.256562       98            FI
## 119: 6.384870       97            FO
## 120: 5.721456       94            FO
##Seleccione las que no finalizan con .d usando (select(!ends with()))
no_d = tib.c %>% select(!ends_with(".d"))
data.table(no_d)
##       Biomasa Flores.r    plaga Estatus Fertilizacion
##   1: 5.346747      101 presente       S            FI
##   2: 5.100974       98 presente       S            FI
##   3: 4.019105       95  ausente       S            FI
##   4: 5.937088       97 presente       S            FO
##   5: 6.011233       98  ausente       S            FO
##  ---                                                 
## 116: 6.153520       81 presente      MA            FI
## 117: 4.289028       97 presente      MA            FO
## 118: 5.256562       98  ausente      MA            FI
## 119: 6.384870       97  ausente      MA            FO
## 120: 5.721456       94  ausente      MA            FO
##Seleccione las que comienzan con Fl usando (select(starts with()))
empezar_fl = tib.c %>% select(starts_with("Fl"))
data.table(empezar_fl)
##      Flores.r Flores.d
##   1:      101        9
##   2:       98       18
##   3:       95       13
##   4:       97        9
##   5:       98        9
##  ---                  
## 116:       81        9
## 117:       97       11
## 118:       98       14
## 119:       97       13
## 120:       94       12
##Seleccione las que comienzan con F y terminan con .d usando (select(starts with()& ends with()))
f_d = tib.c %>% select(starts_with("F")& ends_with("d"))
data.table(f_d)
##      Flores.d
##   1:        9
##   2:       18
##   3:       13
##   4:        9
##   5:        9
##  ---         
## 116:        9
## 117:       11
## 118:       14
## 119:       13
## 120:       12
##Seleccione una variable cualquiera con (select()) para tib.c y agr ́upela por Estatus usando ( group by() )
var_estatus = tib.c %>% group_by(Estatus) %>% select(Flores.r)
## Adding missing grouping variables: `Estatus`
var_estatus
## # A tibble: 120 x 2
## # Groups:   Estatus [3]
##    Estatus Flores.r
##    <fct>      <int>
##  1 S            101
##  2 S             98
##  3 S             95
##  4 S             97
##  5 S             98
##  6 S             97
##  7 S             88
##  8 S             95
##  9 S             96
## 10 S             92
## # ... with 110 more rows
##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
ordenen = tib.c%>% 
  arrange(desc(var_estatus),.by_group = TRUE)
data.table(ordenen)
##       Biomasa Flores.r Flores.d Hojas.d    plaga Estatus Fertilizacion
##   1: 4.560324      101       15     119 presente      MA            FI
##   2: 6.550330      101       12     270  ausente      MA            FI
##   3: 4.705783       98       14     146 presente      MA            FI
##   4: 4.392467       98        8     116 presente      MA            FO
##   5: 5.088211       98       13     165  ausente      MA            FO
##  ---                                                                  
## 116: 4.623619       88        8      67 presente       S            FI
## 117: 4.856259       84       10     171  ausente       S            FO
## 118: 5.715279       84       13     123  ausente       S            FI
## 119: 5.307613       81       15     136  ausente       S            FO
## 120: 4.239475       81       10     173 presente       S            FI
##Filtre los datos que comienzan con Flores para el estatus muy afectadas ( Filter(,) )
filtro =tib.c %>% 
  filter(Estatus == "MA") %>%
  select(starts_with("flores"))
data.table(head(filtro))
##    Flores.r Flores.d
## 1:      101       15
## 2:       98       14
## 3:       95       11
## 4:       97       10
## 5:       98        8
## 6:       97       14
##Filtre los datos para cuando la biomasa es superior a 5 gramos
mayor_5 = tib.c %>% filter(Biomasa > 5)
data.table(head(mayor_5))
##     Biomasa Flores.r Flores.d Hojas.d    plaga Estatus Fertilizacion
## 1: 5.346747      101        9     278 presente       S            FI
## 2: 5.100974       98       18      81 presente       S            FI
## 3: 5.937088       97        9     200 presente       S            FO
## 4: 6.011233       98        9      56  ausente       S            FO
## 5: 5.758177       97        8     188 presente       S            FI
## 6: 6.272009       88       14      65 presente       S            FI
##Filtre los datos para tener solo fertilizacíon se tienen plantas parcialmente afectadas y fueron tratadas con fertilizacíon organicai 

filtro_ferti = tib.c %>% 
  filter(Estatus == "PA" & Fertilizacion == "FO")
data.table(filtro_ferti)
##      Biomasa Flores.r Flores.d Hojas.d    plaga Estatus Fertilizacion
##  1: 4.769820       97       17     103 presente      PA            FO
##  2: 6.069715       97       20     205  ausente      PA            FO
##  3: 4.892479       96        9     280  ausente      PA            FO
##  4: 5.778459       92        8     159 presente      PA            FO
##  5: 4.737926       95        9     295 presente      PA            FO
##  6: 4.714742       93        8     281  ausente      PA            FO
##  7: 5.459188       92       11      39 presente      PA            FO
##  8: 6.973507       97        9     102 presente      PA            FO
##  9: 4.917341       98        8     251 presente      PA            FO
## 10: 5.232343       97       12     134  ausente      PA            FO
## 11: 4.754281       93        7     215 presente      PA            FO
## 12: 4.220727       97       10     150  ausente      PA            FO
## 13: 4.776650       94       20     132 presente      PA            FO
##Filtre los datos para tener solo fertilizaci ́on se tienen plantas parcialmente afectadas ofueron tratadas con fertilizaci ́on inorg ́anica

filter_plaga = tib.c %>%
  filter(Estatus == "PA" | Fertilizacion == "FI")
data.table(head(filter_plaga))
##     Biomasa Flores.r Flores.d Hojas.d    plaga Estatus Fertilizacion
## 1: 5.346747      101        9     278 presente       S            FI
## 2: 5.100974       98       18      81 presente       S            FI
## 3: 4.019105       95       13      64  ausente       S            FI
## 4: 5.758177       97        8     188 presente       S            FI
## 5: 6.272009       88       14      65 presente       S            FI
## 6: 4.499090       95       11     292 presente       S            FI
##Filtre los datos de Flores desprendidas por presencia o ausencia de plaga para cuando el n ́umero de flores desprendidas es mayor a su mediana

filter_flores = tib.c %>% 
  filter(Flores.d > median(Flores.d)) %>%
  select(plaga, Flores.d)
data.table(head(filter_flores))
##       plaga Flores.d
## 1: presente       18
## 2:  ausente       13
## 3: presente       14
## 4: presente       15
## 5: presente       12
## 6:  ausente       15
##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)
Completo = tib.c %>% filter(Hojas.d > median(Hojas.d))%>% 
  select(plaga, Hojas.d)
Incompleto = tib.i %>% filter(Hojas.d > median(Hojas.d)) %>%
  select(plaga, Hojas.d)
median(Completo$Hojas.d, na.rm = TRUE)
## [1] 215.5
median(Incompleto$Hojas.d, na.rm = TRUE)
## [1] NA
##Seleccione dos variables cuantitativas y as ́ıgnelas a un vector con c(,) y n ́ombrelocon v1. Ponga en otro vector dos valores num ́ericos que sirvan de condici ́on a ca-da variable, por ejemplo, el cuartil inferior para una y el cuartil superior para la otra y ll ́amelo v2. Usando ahora pipes, llame el data.frame completo y filtre usan-do el operador punto para seleccionar aquellos datos que superan respectivamen-te cada variable el valor de la condici ́on.

vector1 = tib.c %>% select(Flores.d)
vector2 = tib.c %>% select(Flores.r)
v1 <- c(vector1, vector2)
attach(tib.c)
quantile(Flores.d) # cuartil inferior 8.75
##    0%   25%   50%   75%  100% 
##  4.00  8.75 11.00 13.00 20.00
quantile(Flores.r)# cuartil superior 97
##   0%  25%  50%  75% 100% 
##   81   92   95   97  101
v2 <- c (8.75 , 97)
v1=c();v2=c(); tib.c %>% filter(.[v1[1]]>v2[1],.[v1[2]]>v2[2])
## [1] Biomasa       Flores.r      Flores.d      Hojas.d       plaga        
## [6] Estatus       Fertilizacion
## <0 rows> (or 0-length row.names)

Actividad 4

##Cree un data frame o tableta con todas las variables cuantitativas cont́ınuas estandarizadas con el ✭✭score z✮✮ y las discretas con la estadarizaci ́on minimax y ll ́amelo tib.e.Use (mutate())

V_continua =tib.c %>% mutate(Biomasa =(Biomasa - mean(Biomasa)/sd(Biomasa)))
v_r= tib.c %>% mutate(Flores.r = (Flores.r - min(Flores.r))/(max(Flores.r) - min(Flores.r)))
v_d = tib.c%>% mutate(Flores.d=(Flores.d - min(Flores.d))/(max(Flores.d)-min(Flores.d)))
v_h = tib.c %>% mutate(Hojas.d = (Hojas.d- min(Hojas.d))/(max(Hojas.d)-min(Hojas.d)))

tib.e =data.frame(V_continua, v_r,v_d, v_h)
data.table(tib.e)
##          Biomasa Flores.r Flores.d Hojas.d    plaga Estatus Fertilizacion
##   1: -0.49194851      101        9     278 presente       S            FI
##   2: -0.73772141       98       18      81 presente       S            FI
##   3: -1.81958984       95       13      64  ausente       S            FI
##   4:  0.09839225       97        9     200 presente       S            FO
##   5:  0.17253768       98        9      56  ausente       S            FO
##  ---                                                                     
## 116:  0.31482470       81        9     220 presente      MA            FI
## 117: -1.54966742       97       11     139 presente      MA            FO
## 118: -0.58213375       98       14      32  ausente      MA            FI
## 119:  0.54617486       97       13     214  ausente      MA            FO
## 120: -0.11723947       94       12     273  ausente      MA            FO
##      Biomasa.1 Flores.r.1 Flores.d.1 Hojas.d.1  plaga.1 Estatus.1
##   1:  5.346747       1.00          9       278 presente         S
##   2:  5.100974       0.85         18        81 presente         S
##   3:  4.019105       0.70         13        64  ausente         S
##   4:  5.937088       0.80          9       200 presente         S
##   5:  6.011233       0.85          9        56  ausente         S
##  ---                                                             
## 116:  6.153520       0.00          9       220 presente        MA
## 117:  4.289028       0.80         11       139 presente        MA
## 118:  5.256562       0.85         14        32  ausente        MA
## 119:  6.384870       0.80         13       214  ausente        MA
## 120:  5.721456       0.65         12       273  ausente        MA
##      Fertilizacion.1 Biomasa.2 Flores.r.2 Flores.d.2 Hojas.d.2  plaga.2
##   1:              FI  5.346747        101     0.3125       278 presente
##   2:              FI  5.100974         98     0.8750        81 presente
##   3:              FI  4.019105         95     0.5625        64  ausente
##   4:              FO  5.937088         97     0.3125       200 presente
##   5:              FO  6.011233         98     0.3125        56  ausente
##  ---                                                                   
## 116:              FI  6.153520         81     0.3125       220 presente
## 117:              FO  4.289028         97     0.4375       139 presente
## 118:              FI  5.256562         98     0.6250        32  ausente
## 119:              FO  6.384870         97     0.5625       214  ausente
## 120:              FO  5.721456         94     0.5000       273  ausente
##      Estatus.2 Fertilizacion.2 Biomasa.3 Flores.r.3 Flores.d.3 Hojas.d.3
##   1:         S              FI  5.346747        101          9 0.9326599
##   2:         S              FI  5.100974         98         18 0.2693603
##   3:         S              FI  4.019105         95         13 0.2121212
##   4:         S              FO  5.937088         97          9 0.6700337
##   5:         S              FO  6.011233         98          9 0.1851852
##  ---                                                                    
## 116:        MA              FI  6.153520         81          9 0.7373737
## 117:        MA              FO  4.289028         97         11 0.4646465
## 118:        MA              FI  5.256562         98         14 0.1043771
## 119:        MA              FO  6.384870         97         13 0.7171717
## 120:        MA              FO  5.721456         94         12 0.9158249
##       plaga.3 Estatus.3 Fertilizacion.3
##   1: presente         S              FI
##   2: presente         S              FI
##   3:  ausente         S              FI
##   4: presente         S              FO
##   5:  ausente         S              FO
##  ---                                   
## 116: presente        MA              FI
## 117: presente        MA              FO
## 118:  ausente        MA              FI
## 119:  ausente        MA              FO
## 120:  ausente        MA              FO
##Cree una nueva variable para tib.c donde divida el n ́umero de flores en las ramas con el n ́umero de flores desprendidas.


division_flores = tib.c%>% select(plaga, Flores.d, Flores.r) %>%
  summarise(Flores.r/Flores.d)
data.table(division_flores)
##      Flores.r/Flores.d
##   1:         11.222222
##   2:          5.444444
##   3:          7.307692
##   4:         10.777778
##   5:         10.888889
##  ---                  
## 116:          9.000000
## 117:          8.818182
## 118:          7.000000
## 119:          7.461538
## 120:          7.833333
##Seleccione solo la variable del cociente previo agrupada por plaga. Cree una nueva ariable que imprima el rango m ́ınimo en cada grupo. LLame a la variable rangomin

x = division_flores %>% mutate(rangomin = min_rank(desc(division_flores)))
## Warning in xtfrm.data.frame(x): cannot xtfrm data frames
data.table(x)
##      Flores.r/Flores.d rangomin
##   1:         11.222222       30
##   2:          5.444444      116
##   3:          7.307692       91
##   4:         10.777778       35
##   5:         10.888889       33
##  ---                           
## 116:          9.000000       57
## 117:          8.818182       59
## 118:          7.000000       93
## 119:          7.461538       88
## 120:          7.833333       84
##Renombre las variables asociadas a las flores a su gusto. Use (rename(tib.e,))

new_tib.e = rename(tib.e, Flores.c = Flores.d, flores.3 = Flores.r)
data.table(new_tib.e)
##          Biomasa flores.3 Flores.c Hojas.d    plaga Estatus Fertilizacion
##   1: -0.49194851      101        9     278 presente       S            FI
##   2: -0.73772141       98       18      81 presente       S            FI
##   3: -1.81958984       95       13      64  ausente       S            FI
##   4:  0.09839225       97        9     200 presente       S            FO
##   5:  0.17253768       98        9      56  ausente       S            FO
##  ---                                                                     
## 116:  0.31482470       81        9     220 presente      MA            FI
## 117: -1.54966742       97       11     139 presente      MA            FO
## 118: -0.58213375       98       14      32  ausente      MA            FI
## 119:  0.54617486       97       13     214  ausente      MA            FO
## 120: -0.11723947       94       12     273  ausente      MA            FO
##      Biomasa.1 Flores.r.1 Flores.d.1 Hojas.d.1  plaga.1 Estatus.1
##   1:  5.346747       1.00          9       278 presente         S
##   2:  5.100974       0.85         18        81 presente         S
##   3:  4.019105       0.70         13        64  ausente         S
##   4:  5.937088       0.80          9       200 presente         S
##   5:  6.011233       0.85          9        56  ausente         S
##  ---                                                             
## 116:  6.153520       0.00          9       220 presente        MA
## 117:  4.289028       0.80         11       139 presente        MA
## 118:  5.256562       0.85         14        32  ausente        MA
## 119:  6.384870       0.80         13       214  ausente        MA
## 120:  5.721456       0.65         12       273  ausente        MA
##      Fertilizacion.1 Biomasa.2 Flores.r.2 Flores.d.2 Hojas.d.2  plaga.2
##   1:              FI  5.346747        101     0.3125       278 presente
##   2:              FI  5.100974         98     0.8750        81 presente
##   3:              FI  4.019105         95     0.5625        64  ausente
##   4:              FO  5.937088         97     0.3125       200 presente
##   5:              FO  6.011233         98     0.3125        56  ausente
##  ---                                                                   
## 116:              FI  6.153520         81     0.3125       220 presente
## 117:              FO  4.289028         97     0.4375       139 presente
## 118:              FI  5.256562         98     0.6250        32  ausente
## 119:              FO  6.384870         97     0.5625       214  ausente
## 120:              FO  5.721456         94     0.5000       273  ausente
##      Estatus.2 Fertilizacion.2 Biomasa.3 Flores.r.3 Flores.d.3 Hojas.d.3
##   1:         S              FI  5.346747        101          9 0.9326599
##   2:         S              FI  5.100974         98         18 0.2693603
##   3:         S              FI  4.019105         95         13 0.2121212
##   4:         S              FO  5.937088         97          9 0.6700337
##   5:         S              FO  6.011233         98          9 0.1851852
##  ---                                                                    
## 116:        MA              FI  6.153520         81          9 0.7373737
## 117:        MA              FO  4.289028         97         11 0.4646465
## 118:        MA              FI  5.256562         98         14 0.1043771
## 119:        MA              FO  6.384870         97         13 0.7171717
## 120:        MA              FO  5.721456         94         12 0.9158249
##       plaga.3 Estatus.3 Fertilizacion.3
##   1: presente         S              FI
##   2: presente         S              FI
##   3:  ausente         S              FI
##   4: presente         S              FO
##   5:  ausente         S              FO
##  ---                                   
## 116: presente        MA              FI
## 117: presente        MA              FO
## 118:  ausente        MA              FI
## 119:  ausente        MA              FO
## 120:  ausente        MA              FO
##pase a may ́usculas todos los nombres de las variables en cualquier tibble. Use(rename with(tib.e,toupper))

mayusculas = rename_with(tib.e, toupper)
data.table(mayusculas)
##          BIOMASA FLORES.R FLORES.D HOJAS.D    PLAGA ESTATUS FERTILIZACION
##   1: -0.49194851      101        9     278 presente       S            FI
##   2: -0.73772141       98       18      81 presente       S            FI
##   3: -1.81958984       95       13      64  ausente       S            FI
##   4:  0.09839225       97        9     200 presente       S            FO
##   5:  0.17253768       98        9      56  ausente       S            FO
##  ---                                                                     
## 116:  0.31482470       81        9     220 presente      MA            FI
## 117: -1.54966742       97       11     139 presente      MA            FO
## 118: -0.58213375       98       14      32  ausente      MA            FI
## 119:  0.54617486       97       13     214  ausente      MA            FO
## 120: -0.11723947       94       12     273  ausente      MA            FO
##      BIOMASA.1 FLORES.R.1 FLORES.D.1 HOJAS.D.1  PLAGA.1 ESTATUS.1
##   1:  5.346747       1.00          9       278 presente         S
##   2:  5.100974       0.85         18        81 presente         S
##   3:  4.019105       0.70         13        64  ausente         S
##   4:  5.937088       0.80          9       200 presente         S
##   5:  6.011233       0.85          9        56  ausente         S
##  ---                                                             
## 116:  6.153520       0.00          9       220 presente        MA
## 117:  4.289028       0.80         11       139 presente        MA
## 118:  5.256562       0.85         14        32  ausente        MA
## 119:  6.384870       0.80         13       214  ausente        MA
## 120:  5.721456       0.65         12       273  ausente        MA
##      FERTILIZACION.1 BIOMASA.2 FLORES.R.2 FLORES.D.2 HOJAS.D.2  PLAGA.2
##   1:              FI  5.346747        101     0.3125       278 presente
##   2:              FI  5.100974         98     0.8750        81 presente
##   3:              FI  4.019105         95     0.5625        64  ausente
##   4:              FO  5.937088         97     0.3125       200 presente
##   5:              FO  6.011233         98     0.3125        56  ausente
##  ---                                                                   
## 116:              FI  6.153520         81     0.3125       220 presente
## 117:              FO  4.289028         97     0.4375       139 presente
## 118:              FI  5.256562         98     0.6250        32  ausente
## 119:              FO  6.384870         97     0.5625       214  ausente
## 120:              FO  5.721456         94     0.5000       273  ausente
##      ESTATUS.2 FERTILIZACION.2 BIOMASA.3 FLORES.R.3 FLORES.D.3 HOJAS.D.3
##   1:         S              FI  5.346747        101          9 0.9326599
##   2:         S              FI  5.100974         98         18 0.2693603
##   3:         S              FI  4.019105         95         13 0.2121212
##   4:         S              FO  5.937088         97          9 0.6700337
##   5:         S              FO  6.011233         98          9 0.1851852
##  ---                                                                    
## 116:        MA              FI  6.153520         81          9 0.7373737
## 117:        MA              FO  4.289028         97         11 0.4646465
## 118:        MA              FI  5.256562         98         14 0.1043771
## 119:        MA              FO  6.384870         97         13 0.7171717
## 120:        MA              FO  5.721456         94         12 0.9158249
##       PLAGA.3 ESTATUS.3 FERTILIZACION.3
##   1: presente         S              FI
##   2: presente         S              FI
##   3:  ausente         S              FI
##   4: presente         S              FO
##   5:  ausente         S              FO
##  ---                                   
## 116: presente        MA              FI
## 117: presente        MA              FO
## 118:  ausente        MA              FI
## 119:  ausente        MA              FO
## 120:  ausente        MA              FO
##Pase a min ́usculas todos los nombres de variable y aprove-che y cambie los .d por guiones al piso, es decir, d. Use(rename with(tib.e,∼tolower(gsub(”.”,” ”,.x,fixed=TRUE))))
renombre = rename_with(tib.e,∼tolower(gsub(".","_",.x, fixed = TRUE)))
data.table(renombre)
##          biomasa flores_r flores_d hojas_d    plaga estatus fertilizacion
##   1: -0.49194851      101        9     278 presente       S            FI
##   2: -0.73772141       98       18      81 presente       S            FI
##   3: -1.81958984       95       13      64  ausente       S            FI
##   4:  0.09839225       97        9     200 presente       S            FO
##   5:  0.17253768       98        9      56  ausente       S            FO
##  ---                                                                     
## 116:  0.31482470       81        9     220 presente      MA            FI
## 117: -1.54966742       97       11     139 presente      MA            FO
## 118: -0.58213375       98       14      32  ausente      MA            FI
## 119:  0.54617486       97       13     214  ausente      MA            FO
## 120: -0.11723947       94       12     273  ausente      MA            FO
##      biomasa_1 flores_r_1 flores_d_1 hojas_d_1  plaga_1 estatus_1
##   1:  5.346747       1.00          9       278 presente         S
##   2:  5.100974       0.85         18        81 presente         S
##   3:  4.019105       0.70         13        64  ausente         S
##   4:  5.937088       0.80          9       200 presente         S
##   5:  6.011233       0.85          9        56  ausente         S
##  ---                                                             
## 116:  6.153520       0.00          9       220 presente        MA
## 117:  4.289028       0.80         11       139 presente        MA
## 118:  5.256562       0.85         14        32  ausente        MA
## 119:  6.384870       0.80         13       214  ausente        MA
## 120:  5.721456       0.65         12       273  ausente        MA
##      fertilizacion_1 biomasa_2 flores_r_2 flores_d_2 hojas_d_2  plaga_2
##   1:              FI  5.346747        101     0.3125       278 presente
##   2:              FI  5.100974         98     0.8750        81 presente
##   3:              FI  4.019105         95     0.5625        64  ausente
##   4:              FO  5.937088         97     0.3125       200 presente
##   5:              FO  6.011233         98     0.3125        56  ausente
##  ---                                                                   
## 116:              FI  6.153520         81     0.3125       220 presente
## 117:              FO  4.289028         97     0.4375       139 presente
## 118:              FI  5.256562         98     0.6250        32  ausente
## 119:              FO  6.384870         97     0.5625       214  ausente
## 120:              FO  5.721456         94     0.5000       273  ausente
##      estatus_2 fertilizacion_2 biomasa_3 flores_r_3 flores_d_3 hojas_d_3
##   1:         S              FI  5.346747        101          9 0.9326599
##   2:         S              FI  5.100974         98         18 0.2693603
##   3:         S              FI  4.019105         95         13 0.2121212
##   4:         S              FO  5.937088         97          9 0.6700337
##   5:         S              FO  6.011233         98          9 0.1851852
##  ---                                                                    
## 116:        MA              FI  6.153520         81          9 0.7373737
## 117:        MA              FO  4.289028         97         11 0.4646465
## 118:        MA              FI  5.256562         98         14 0.1043771
## 119:        MA              FO  6.384870         97         13 0.7171717
## 120:        MA              FO  5.721456         94         12 0.9158249
##       plaga_3 estatus_3 fertilizacion_3
##   1: presente         S              FI
##   2: presente         S              FI
##   3:  ausente         S              FI
##   4: presente         S              FO
##   5:  ausente         S              FO
##  ---                                   
## 116: presente        MA              FI
## 117: presente        MA              FO
## 118:  ausente        MA              FI
## 119:  ausente        MA              FO
## 120:  ausente        MA              FO

Actividad 5

##Seleccione la variable biomasa de la tableta con faltantes y con summarise() obtenga la media y el n ́umero de datos de esta variable.

resumen <- tib.i %>% 
  select(Biomasa)%>%
  summarise(med_bio = mean(Biomasa), n_datos = n())
data.table(resumen)
##     med_bio n_datos
## 1: 5.089148      90
##Seleccione la variable biomasa de la tableta con faltantes y con summarise() obtengala media y el n ́umero de datos por tipo de fertilizaci ́on.

tib.i%>%
  group_by(Fertilizacion) %>%
  select(Biomasa)%>% 
  summarise(media = mean(Biomasa),n_datos =n())
## Adding missing grouping variables: `Fertilizacion`
## # A tibble: 2 x 3
##   Fertilizacion media n_datos
##   <chr>         <dbl>   <int>
## 1 FI             5.01      58
## 2 FO             5.23      32
##Seleccione la variable biomasa de la tableta con faltantes y con summarise() obtengalos cuantiles 0.10,0.20,0.30,0.40 y 0.50 por tipo de fertilizaci ́on.

tib.i %>%
  group_by(Fertilizacion)%>%
  select(Biomasa)%>%
  summarise(Q_1 = quantile(Biomasa, 0.10),
            Q_2 = quantile(Biomasa, 0.20),
            Q_3 = quantile(Biomasa, 0.30),
            Q_4 = quantile(Biomasa, 0.40),
            Q_5 = quantile(Biomasa, 0.50 ) )
## Adding missing grouping variables: `Fertilizacion`
## # A tibble: 2 x 6
##   Fertilizacion   Q_1   Q_2   Q_3   Q_4   Q_5
##   <chr>         <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 FI             3.94  4.17  4.68  4.91  5.12
## 2 FO             4.41  4.72  4.77  4.90  5.04
##Seleccione la variable biomasa de la tableta con faltantes y con summarise() obtenga lamedia, mediana, m ́aximo, m ́ınimo, desviaci ́on t ́ıpica, desviaci ́on media, media truncada y varianza por tipo de fertilizaci ́on y plaga.

tib.i %>%
  group_by(Fertilizacion, plaga)%>%
  select(Biomasa)%>%
  summarise(med_bio = mean(Biomasa),
            mediana_bio = median(Biomasa),
            min_bio = min(Biomasa),
            max_bio = max(Biomasa),
            desv_bio = sd(Biomasa),
            desv_media = mad(Biomasa),
            media_truncada = mean(Biomasa, trim = 0.1),
            var_bio = var(Biomasa))
## Adding missing grouping variables: `Fertilizacion`, `plaga`
## `summarise()` has grouped output by 'Fertilizacion'. You can override using the `.groups` argument.
## # A tibble: 4 x 10
## # Groups:   Fertilizacion [2]
##   Fertilizacion plaga    med_bio mediana_bio min_bio max_bio desv_bio desv_media
##   <chr>         <chr>      <dbl>       <dbl>   <dbl>   <dbl>    <dbl>      <dbl>
## 1 FI            ausente     4.86        4.97    3.33    6.06    0.850      1.04 
## 2 FI            presente    5.05        5.12    2.55    6.84    0.920      0.945
## 3 FO            ausente     5.25        5.23    3.74    6.55    0.916      1.15 
## 4 FO            presente    5.22        4.92    4.29    6.97    0.716      0.521
## # ... with 2 more variables: media_truncada <dbl>, var_bio <dbl>
##Con la tableta con faltantes use la funci ́on drop na() para sacar los faltantes y comparelas estad ́ısticas obtenidas en el item anterior con y sin faltantes

faltantes <- drop_na(tib.i)
data.table(head(faltantes))
##     Biomasa Flores.r Flores.d Hojas.d    plaga Estatus Fertilizacion
## 1: 5.937088       97        9     200 presente       S            FO
## 2: 2.835715       94       11      94 presente      MA            FI
## 3: 5.654898       95       17     129 presente      MA            FI
## 4: 5.913560       97        9      91 presente       S            FI
## 5: 5.482667       97        8     241 presente       S            FI
## 6: 4.151177       88       13     141 presente      MA            FI
##Filtre los datos seleccionando solo las plantas afectadas o muy afectadas.Use el operador %in %

dentro = tib.c %>%
  filter(Estatus %in% c("MA","PA"))
data.table(head(dentro))
##     Biomasa Flores.r Flores.d Hojas.d    plaga Estatus Fertilizacion
## 1: 5.129022      101        9     126 presente      PA            FI
## 2: 4.917921       98        4       6 presente      PA            FI
## 3: 5.799616       95       14       1 presente      PA            FI
## 4: 4.769820       97       17     103 presente      PA            FO
## 5: 4.148751       98        8     157  ausente      PA            FI
## 6: 6.069715       97       20     205  ausente      PA            FO
##Seleccione la tibble con faltantes y use complete.cases(.) para dejar por fuera los faltantes

sin_na <- tib.i[complete.cases(tib.i),]
data.table(head(sin_na))
##     Biomasa Flores.r Flores.d Hojas.d    plaga Estatus Fertilizacion
## 1: 5.937088       97        9     200 presente       S            FO
## 2: 2.835715       94       11      94 presente      MA            FI
## 3: 5.654898       95       17     129 presente      MA            FI
## 4: 5.913560       97        9      91 presente       S            FI
## 5: 5.482667       97        8     241 presente       S            FI
## 6: 4.151177       88       13     141 presente      MA            FI
##Elimine de cualquier tibble las columnas asociadas al conteo de flores
borrar <- c("plaga")
datos2 <- tib.c[ , !(names(tib.c) %in% borrar)]
head(datos2, n=9)
##    Biomasa Flores.r Flores.d Hojas.d Estatus Fertilizacion
## 1 5.346747      101        9     278       S            FI
## 2 5.100974       98       18      81       S            FI
## 3 4.019105       95       13      64       S            FI
## 4 5.937088       97        9     200       S            FO
## 5 6.011233       98        9      56       S            FO
## 6 5.758177       97        8     188       S            FI
## 7 6.272009       88       14      65       S            FI
## 8 4.499090       95       11     292       S            FI
## 9 5.995158       96       10      66       S            FI
data.table(datos2)
##       Biomasa Flores.r Flores.d Hojas.d Estatus Fertilizacion
##   1: 5.346747      101        9     278       S            FI
##   2: 5.100974       98       18      81       S            FI
##   3: 4.019105       95       13      64       S            FI
##   4: 5.937088       97        9     200       S            FO
##   5: 6.011233       98        9      56       S            FO
##  ---                                                         
## 116: 6.153520       81        9     220      MA            FI
## 117: 4.289028       97       11     139      MA            FO
## 118: 5.256562       98       14      32      MA            FI
## 119: 6.384870       97       13     214      MA            FO
## 120: 5.721456       94       12     273      MA            FO
##Reordene una tableta usando select(,everything()) colocando primero los conteos de flores

reordene = tib.c %>% select(Flores.r, Flores.d, everything())
data.table(reordene)
##      Flores.r Flores.d  Biomasa Hojas.d    plaga Estatus Fertilizacion
##   1:      101        9 5.346747     278 presente       S            FI
##   2:       98       18 5.100974      81 presente       S            FI
##   3:       95       13 4.019105      64  ausente       S            FI
##   4:       97        9 5.937088     200 presente       S            FO
##   5:       98        9 6.011233      56  ausente       S            FO
##  ---                                                                  
## 116:       81        9 6.153520     220 presente      MA            FI
## 117:       97       11 4.289028     139 presente      MA            FO
## 118:       98       14 5.256562      32  ausente      MA            FI
## 119:       97       13 6.384870     214  ausente      MA            FO
## 120:       94       12 5.721456     273  ausente      MA            FO