R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

Instalamos la librería (paquete) para importar el archivo de Excel

Y subimos el archivo a RStudio

install.packages('xlsx', repos = 'https://cran.rstudio.com/')
## Installing package into 'C:/Users/gonza/AppData/Local/R/win-library/4.4'
## (as 'lib' is unspecified)
## package 'xlsx' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'xlsx'
## Warning: restored 'xlsx'
## 
## The downloaded binary packages are in
##  C:\Users\gonza\AppData\Local\Temp\RtmpU14fiR\downloaded_packages
library(xlsx)
## Warning: package 'xlsx' was built under R version 4.4.3
Shrimp_Data_Gonzalo <- read.xlsx(file = 'C:\\Users\\gonza\\OneDrive\\Desktop\\Gonzalo_ASUS_laptop\\Gonzalo_TRABAJO\\UOC_Univ_Oberta_Catalunya\\Asignaturas\\02_SAD_Software_Analisis_Datos\\Actividades_SAD\\Reto_01\\archivos_LAB1_y_LAB2\\Shrimp_Data_Gonzalo.xlsx', sheetName = 'DATOS')

Vamos a visualizar cómo han sido exportados los datos

head(Shrimp_Data_Gonzalo)
##     ID NA.              ESPECIE PRESENT PR_COLA   TRAT LONG_TOTAL LONG_COLA
## 1 R001   1 Penaeus stylirostris  Entero    <NA>  Crudo        113        81
## 2 R010   2      Penaeus monodon  Entero    <NA>  Crudo        167       112
## 3 R100   3    Penaeus japonicus  Entero    <NA>  Crudo        155       124
## 4 R101   4    Penaeus japonicus  Entero    <NA>  Crudo        152        31
## 5 R102   5    Penaeus japonicus    Cola  Pelado Cosido         72        72
## 6 R103   6    Penaeus japonicus    Cola  Pelado Cocido         88        88
##   PESO ASPECTO      PAIS NA..1 NA..2 NA..3 NA..4 NA..5 NA..6 NA..7 NA..8
## 1 19.6   BUENO Nicaragua    NA    NA    NA    NA    NA    NA    NA    NA
## 2 19.2   BUENO   Vietnam    NA    NA    NA    NA    NA    NA    NA    NA
## 3 35.6   BUENO    España    NA    NA    NA    NA    NA    NA    NA    NA
## 4 17.5   BUENO    España    NA    NA    NA    NA    NA    NA    NA    NA
## 5  9.9   BUENO Tailandia    NA    NA    NA    NA    NA    NA    NA    NA
## 6   83   BUENO Tailandia    NA    NA    NA    NA    NA    NA    NA    NA

Y la estructura de las variables

str(Shrimp_Data_Gonzalo)
## 'data.frame':    300 obs. of  19 variables:
##  $ ID        : chr  "R001" "R010" "R100" "R101" ...
##  $ NA.       : num  1 2 3 4 5 6 7 8 9 10 ...
##  $ ESPECIE   : chr  "Penaeus stylirostris" "Penaeus monodon" "Penaeus japonicus" "Penaeus japonicus" ...
##  $ PRESENT   : chr  "Entero" "Entero" "Entero" "Entero" ...
##  $ PR_COLA   : chr  NA NA NA NA ...
##  $ TRAT      : chr  "Crudo" "Crudo" "Crudo" "Crudo" ...
##  $ LONG_TOTAL: chr  "113" "167" "155" "152" ...
##  $ LONG_COLA : chr  "81" "112" "124" "31" ...
##  $ PESO      : chr  "19.6" "19.2" "35.6" "17.5" ...
##  $ ASPECTO   : chr  "BUENO" "BUENO" "BUENO" "BUENO" ...
##  $ PAIS      : chr  "Nicaragua" "Vietnam" "España" "España" ...
##  $ NA..1     : logi  NA NA NA NA NA NA ...
##  $ NA..2     : logi  NA NA NA NA NA NA ...
##  $ NA..3     : logi  NA NA NA NA NA NA ...
##  $ NA..4     : logi  NA NA NA NA NA NA ...
##  $ NA..5     : logi  NA NA NA NA NA NA ...
##  $ NA..6     : logi  NA NA NA NA NA NA ...
##  $ NA..7     : logi  NA NA NA NA NA NA ...
##  $ NA..8     : logi  NA NA NA NA NA NA ...
class(Shrimp_Data_Gonzalo$ESPECIE)
## [1] "character"
class(Shrimp_Data_Gonzalo$PRESENT)
## [1] "character"
class(Shrimp_Data_Gonzalo$PR_COLA)
## [1] "character"
class(Shrimp_Data_Gonzalo$TRAT)
## [1] "character"
class(Shrimp_Data_Gonzalo$LONG_TOTAL)
## [1] "character"
class(Shrimp_Data_Gonzalo$LONG_COLA)
## [1] "character"
class(Shrimp_Data_Gonzalo$PESO)
## [1] "character"
class(Shrimp_Data_Gonzalo$ASPECTO)
## [1] "character"
class(Shrimp_Data_Gonzalo$PAIS)
## [1] "character"

Después de transfomar las variables, vamos a volver a hacer un resumen de las variables principales de estudio: (utilizando fivenum())

Voy a omitir los NA values:

Shrimp_Data_Gonzalo <- Shrimp_Data_Gonzalo[!is.na(Shrimp_Data_Gonzalo$LONG_TOTAL), ]

A partir del mensaje de error obtenido, dadas las características de las variables, es necesario transformarlas para que esta función me sirva para hacer un resumen de las variables y se pueda operar adecuadamente con ellas.

Transformación de las variables

Variable PAÍS

Shrimp_Data_Gonzalo$PAIS <- as.factor(Shrimp_Data_Gonzalo$PAIS) # Creates a factor version of PAIS (if not already a factor)
class(Shrimp_Data_Gonzalo$PAIS)
## [1] "factor"
country_labels <- levels(Shrimp_Data_Gonzalo$PAIS) # Stores the mapping
Shrimp_Data_Gonzalo$PAIS <- as.numeric(Shrimp_Data_Gonzalo$PAIS) # Converts PAIS to numeric
# Now, Shrimp_Data_Gonzalo$PAIS contains numbers, and country_labels stores the original country names in the same order as factor levels.
class(Shrimp_Data_Gonzalo$PAIS)
## [1] "numeric"

Transformación de las variables

Variable ESPECIE

Shrimp_Data_Gonzalo$ESPECIE <- as.factor(Shrimp_Data_Gonzalo$ESPECIE) # Creates a factor version of ESPECIE (if not already a factor)
class(Shrimp_Data_Gonzalo$ESPECIE)
## [1] "factor"

Transformación de las variables

Variable PRESENT

Shrimp_Data_Gonzalo$PRESENT <- as.factor(Shrimp_Data_Gonzalo$PRESENT) # Creates a factor version of ESPECIE (if not already a factor)
class(Shrimp_Data_Gonzalo$PRESENT)
## [1] "factor"

Transformación de las variables

Variable PR_COLA

Shrimp_Data_Gonzalo$PR_COLA <- as.factor(Shrimp_Data_Gonzalo$PRESENT) # Creates a factor version of PR_COLA (if not already a factor)
class(Shrimp_Data_Gonzalo$PR_COLA)
## [1] "factor"

Transformación de las variables

Variable TRAT

Shrimp_Data_Gonzalo$TRAT <- as.factor(Shrimp_Data_Gonzalo$PRESENT) # Creates a factor version of TRAT (if not already a factor)
class(Shrimp_Data_Gonzalo$TRAT)
## [1] "factor"

Transformación de las variables

Variable LONG_TOTAL

Shrimp_Data_Gonzalo$LONG_TOTAL <- as.numeric(Shrimp_Data_Gonzalo$LONG_TOTAL)
## Warning: NAs introducidos por coerción
class(Shrimp_Data_Gonzalo$LONG_TOTAL)
## [1] "numeric"

Transformación de las variables

Variable LONG_COLA

Shrimp_Data_Gonzalo$LONG_COLA <- as.numeric(Shrimp_Data_Gonzalo$LONG_COLA) # Creates a numeric version of LONG_COLA (if not already a numeric value)
## Warning: NAs introducidos por coerción
class(Shrimp_Data_Gonzalo$LONG_COLA)
## [1] "numeric"

Transformación de las variables

Variable PESO

Shrimp_Data_Gonzalo$PESO <- as.numeric(Shrimp_Data_Gonzalo$PESO) # Creates a numeric version of PESO (if not already a numeric value)
## Warning: NAs introducidos por coerción
class(Shrimp_Data_Gonzalo$PESO)
## [1] "numeric"

Transformación de las variables

Variable ASPECTO

Shrimp_Data_Gonzalo$ASPECTO <- as.factor(Shrimp_Data_Gonzalo$ASPECTO) # Creates a factor version of ASPECTO (if not already a factor)
class(Shrimp_Data_Gonzalo$ASPECTO)
## [1] "factor"

Después de transfomar las variables, vamos a volver a hacer un resumen de las variables principales de estudio.

Primero, excluimos de la función aquellos valores NA para la variable que los tenga incluidos:

fivenum(Shrimp_Data_Gonzalo$LONG_TOTAL, na.rm = TRUE)
## [1]  29.0  74.0 103.5 136.0 222.0

fivenum(Shrimp_Data_Gonzalo\(LONG_COLA) fivenum(Shrimp_Data_Gonzalo\)PESO)

## Observamos que la función fivenum() nos da un resumen de las variables de tipo numérico (longitud total, longitud de la cola y peso.
## The five-number summary is a set of descriptive statistics that provides information about a dataset. It consists of the five most important sample percentiles: (Ref: Wikipedia)
# 1. the sample minimum (smallest observation)
# 2. the lower quartile or first quartile
# 3. the median (the middle value)
# 4. the upper quartile or third quartile
# 5. the sample maximum (largest observation)
## Como es lógico, np sirve para hacer un resumen de las variables que se han convertido en factores (ESPECIE, PAÍS, etc.). Vamos a utilizar otra función: summary()

``` r
summary(Shrimp_Data_Gonzalo$ESPECIE)
## Litopenaeus vannamei     Penaeus japonics    Penaeus japonicus 
##                   20                    1                   57 
##      Penaeus monodon Penaeus stylirostris  Penaus stylirostris 
##                   38                   43                    1
summary(Shrimp_Data_Gonzalo$PRESENT)
##    Cola      ??    Cola    COLA  Entero Entero  
##       1       1      72       1      84       1
summary(Shrimp_Data_Gonzalo$TRAT)
##    Cola      ??    Cola    COLA  Entero Entero  
##       1       1      72       1      84       1
summary(Shrimp_Data_Gonzalo$ASPECTO)
##       -    BUEN   BUEN0   BUENO    MALO REGULAR 
##       1       1       1     132       3      22
summary(Shrimp_Data_Gonzalo$PAIS)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   1.000   5.000   6.000   5.575   7.000   9.000