Primero la Sección de Librerías de Funciones:
# rownames(installed.packages())
list.of.packages <- c(
"arm" ,
"broom" ,
"corrplot" ,
"cowplot" ,
"datasets" ,
"datasets" ,
"dplyr" ,
"eeptools" ,
"estimatr" ,
"FinCal" ,
"formatR" ,
"ggfortify" ,
"ggpubr" ,
"haven" ,
"Hmisc" ,
"infer" ,
"knitr" ,
"lmtest" ,
"margins" ,
"nycflights13" ,
"psych" ,
"readxl" ,
"reshape2" ,
"rms" ,
"skimr" ,
"stargazer" ,
"stringr" ,
"survival" ,
"tableone" ,
"tidyr" ,
"tidyverse" ,
"TTR" ,
"wooldridge" ,
"xlsx"
)
has <- list.of.packages %in% rownames(installed.packages())
if(any(!has)) install.packages(new.packages[!has])
Llamada a LIBRERIAS:
# library(arm)
# library(broom)
# library(corrplot)
# library(cowplot)
# library(datasets)
library(dplyr)
# library(eeptools)
# library(estimatr)
library(FinCal)
# library(formatR)
# library(ggfortify)
# library(ggpubr)
library(ggplot2)
# library(haven) #para la lectura de archivos DTA de Stata
# library(Hmisc)
# library(infer)
# library(knitr)
# library(lmtest)
# library(margins)
# library(nycflights13)
# library(psych)
library(readxl)
library(reshape2) #para hacer ReShape (Pivot Tables)
# library(rms)
# library(skimr)
# library(stargazer)
# library(stringr)
# library(survival)
# library(tableone)
library(tidyr) #para hacer ReShape (Pivot Tables)
library(tidyverse)
library(TTR) #para las graficas de series de tiempo
# library(wooldridge)
library(xlsx) #para exportar a Excel file
A partir de aquí la Sección de Importación de Datasets:
getwd() #get to show me the current Working Directory
[1] "I:/001.7 CURSO-02 Lenguaje R Semillero/21.09.24.A Proyecto_01"
### Cargando BBDD: n5ay5qadfe7e1nnsv5s01oe1x62mq51j.csv ####
# Version de BBDD: 2021.09.24 v1
# RUTA: https://ibm.box.com/shared/static/
# code to download the dataset
download.file("https://ibm.box.com/shared/static/n5ay5qadfe7e1nnsv5s01oe1x62mq51j.csv", destfile="movies-db.csv")
trying URL 'https://ibm.box.com/shared/static/n5ay5qadfe7e1nnsv5s01oe1x62mq51j.csv'
Content type 'text/csv' length 1424 bytes
downloaded 1424 bytes
movies_data <- read.csv("movies-db.csv", header=TRUE, sep=",")
movies_data
REVISION RAPIDA DEL DATAFRAME:
#View(movies_data)
summary(movies_data) # Summary Estadístico.
name year length_min genre average_rating cost_millions foreign
Length:30 Min. :1936 Min. : 81.00 Length:30 Min. :5.200 Min. : 0.400 Min. :0.0
Class :character 1st Qu.:1988 1st Qu.: 99.25 Class :character 1st Qu.:7.925 1st Qu.: 3.525 1st Qu.:0.0
Mode :character Median :1998 Median :110.50 Mode :character Median :8.300 Median : 13.000 Median :0.0
Mean :1996 Mean :116.80 Mean :8.103 Mean : 22.300 Mean :0.4
3rd Qu.:2008 3rd Qu.:124.25 3rd Qu.:8.500 3rd Qu.: 25.000 3rd Qu.:1.0
Max. :2015 Max. :179.00 Max. :9.300 Max. :165.000 Max. :1.0
age_restriction
Min. : 0.00
1st Qu.:12.00
Median :14.00
Mean :12.93
3rd Qu.:16.00
Max. :18.00
head(movies_data) # Primeros 6.
names(movies_data) # Names de columnas.
[1] "name" "year" "length_min" "genre" "average_rating" "cost_millions" "foreign"
[8] "age_restriction"
print(is.data.frame(movies_data))
[1] TRUE
#attach(movies_data) #only if there is only 1 dataset
# CONTENIDO DE TABLA:
# movies_data es la tabla con datos de películas.
A partir de aquí inicia el Cuerpo del Script:
my_data <- movies_data
akira <- my_data[my_data$name == "Akira", "average_rating"]
akira
[1] 8.1
Creo la Function isGoodRating:
isGoodRating <- function(rating, threshold = 7){
if(rating < threshold){
return("NO") # return NO if the movie rating is less than the threshold
}else{
return("YES") # otherwise return YES
}
}
Llamo a mi function:
isGoodRating(akira)
[1] "YES"
isGoodRating(8, threshold = 8.5)
[1] "NO"
CREACION DE LA FUNCION 2: Now, let’s put this all together into a function, that can take any moviename and return a YES or NO for whether or not we should watch it.
watchMovie <- function(data, moviename){
rating <- data[data["name"] == moviename,"average_rating"]
return(isGoodRating(rating))
}
LLAMADA A LA FUNCION 2:
watchMovie(my_data, "Akira")
[1] "YES"
Make sure you take the time to understand the function above. Notice how the function expects two inputs: data and moviename, and so when we use the function, we must also input two arguments.
But what if we only want to watch really good movies? How do we set our rating threshold that we created earlier? Here’s how:
REDEFINO LA FUNCION watchMovie:
watchMovie <- function(data, moviename, my_threshold){
rating <- data[data$name == moviename,"average_rating"]
return(isGoodRating(rating, threshold = my_threshold))
}
Now our watchMovie takes three inputs: data, moviename and my_threshold:
watchMovie(my_data, "Akira", 7)
[1] "YES"
What if we want to still set our default threshold to be 7? Here’s how we can do it:
DEFINO LA FUNCION watchMOvie por 3ra vez:
watchMovie <- function(data, moviename, my_threshold = 7){
rating <- data[data[,1] == moviename,"average_rating"]
return(isGoodRating(rating, threshold = my_threshold))
}
LLAMO A LA FUNCION 2 (VERSION 3):
watchMovie(my_data,"Akira")
[1] "YES"