Primero la Sección de Librerías de Funciones:
# rownames(installed.packages())
Warning message:
R graphics engine version 14 is not supported by this version of RStudio. The Plots tab will be disabled until a newer version of RStudio is installed.
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"
)
Llamada a LIBRERIAS:
# library(arm)
# library(broom)
# library(corrplot)
# library(cowplot)
# library(datasets)
library(dplyr)
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(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)
Attaching package: 㤼㸱tidyr㤼㸲
The following object is masked from 㤼㸱package:reshape2㤼㸲:
smiths
library(tidyverse)
Registered S3 methods overwritten by 'dbplyr':
method from
print.tbl_lazy
print.tbl_sql
-- Attaching packages ------------------------------------------------------------------------------------------------------------ tidyverse 1.3.1 --
v tibble 3.1.4 v stringr 1.4.0
v readr 2.0.1 v forcats 0.5.1
v purrr 0.3.4
-- Conflicts --------------------------------------------------------------------------------------------------------------- tidyverse_conflicts() --
x dplyr::filter() masks stats::filter()
x dplyr::lag() masks stats::lag()
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.25.R.Lab08-R_Notebook importingData"
### 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 age_restriction
Length:30 Min. :1936 Min. : 81.00 Length:30 Min. :5.200 Min. : 0.400 Min. :0.0 Min. : 0.00
Class :character 1st Qu.:1988 1st Qu.: 99.25 Class :character 1st Qu.:7.925 1st Qu.: 3.525 1st Qu.:0.0 1st Qu.:12.00
Mode :character Median :1998 Median :110.50 Mode :character Median :8.300 Median : 13.000 Median :0.0 Median :14.00
Mean :1996 Mean :116.80 Mean :8.103 Mean : 22.300 Mean :0.4 Mean :12.93
3rd Qu.:2008 3rd Qu.:124.25 3rd Qu.:8.500 3rd Qu.: 25.000 3rd Qu.:1.0 3rd Qu.:16.00
Max. :2015 Max. :179.00 Max. :9.300 Max. :165.000 Max. :1.0 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" "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"