x <- 10:1
sum(x) # suma
## [1] 55
mean(x) # promedio
## [1] 5.5
indu <- c("Health Care", "Financials", "Info Tech", "Materials", "
Industrials")
length(indu) # conteo
## [1] 5
sqrt(9)
## [1] 3
sqrt(1:10)
## [1] 1.000000 1.414214 1.732051 2.000000 2.236068 2.449490 2.645751 2.828427
## [9] 3.000000 3.162278
ingresos <- c(100, 90, 100, 110, 105)
gastos <- c(80, 75, 90, 115, 85)
utilidad <- ingresos - gastos
utilidad
## [1] 20 15 10 -5 20
ingresos_MXN <- c(100, 90, 100, 110, 105)
tdc <- 20.5
ingresos_USD <- ingresos_MXN * tdc
ingresos_USD
## [1] 2050.0 1845.0 2050.0 2255.0 2152.5
ingresos <- c(100, 90, 100, 110, 105)
gastos <- c(80, 75, 90)
utilidad <- ingresos - gastos
## Warning in ingresos - gastos: longitud de objeto mayor no es múltiplo de la
## longitud de uno menor
## Warning in ingresos - gastos: longer object length is not a multiple of shorter
## object length
utilidad
## [1] 20 15 10 30 30
# Recuerda que T es lo mismo que TRUE y F lo mismo que FALSE
ingresos[c(T, F, T, F, T)]
## [1] 100 100 105
tiendas <- c("A", "B", "C", "D", "E")
ingresos <- c(100, 90, 100, 110, 105)
altos <- ingresos > 100
altos
## [1] FALSE FALSE FALSE TRUE TRUE
tiendas[altos]
## [1] "D" "E"
tiendas[altos == TRUE]
## [1] "D" "E"
tiendas[ingresos > 100]
## [1] "D" "E"
tiendas[!altos]
## [1] "A" "B" "C"
tiendas[altos == FALSE]
## [1] "A" "B" "C"
tiendas[ingresos <= 100]
## [1] "A" "B" "C"
ingresos <- c(100, 90, 100, 110, 105)
gastos <- c(80, 75, 90, 115, 85)
rentables <- ingresos > gastos
rentables
## [1] TRUE TRUE TRUE FALSE TRUE
tiendas[rentables]
## [1] "A" "B" "C" "E"
tiendas[ingresos > gastos]
## [1] "A" "B" "C" "E"
p <- c(10, 9, 10, 11, 10)
p[2]/p[1] - 1
## [1] -0.1
\[ r=\left(p_1-p_0\right) / p_0=p_1 / p_0-1 \]
r <- p[2:5]/p[1:4] - 1
r
## [1] -0.10000000 0.11111111 0.10000000 -0.09090909
\[ r=\ln \frac{P_1}{P_{0}}=\ln P_1-\ln P_{0} \]
# recuerda que en R, log es la función para calcular el logaritmo natural
log(p[2:5]/p[1:4])
## [1] -0.10536052 0.10536052 0.09531018 -0.09531018
cf <- 100
n <- 5
i <- seq(0.025, 0.10, by = 0.025)
pv <- cf/(1+i)^n
pv
## [1] 88.38543 78.35262 69.65586 62.09213
plot(x = i, y = pv, type = "l")
qbs <- c("Mahomes", "Garoppolo", "Brady", "Rodgers", "Brees")
teams <- c("Chiefs", "49ers", "Patriots", "Packers", "Saints")
ages <- c(24, 28, 42, 36, 41)
nfl <- data.frame(qbs, teams, ages)
nfl$cities <- c("Kansas", "San Francisco", "New England",
"Green Bay", "New Orleans")
nfl
## qbs teams ages cities
## 1 Mahomes Chiefs 24 Kansas
## 2 Garoppolo 49ers 28 San Francisco
## 3 Brady Patriots 42 New England
## 4 Rodgers Packers 36 Green Bay
## 5 Brees Saints 41 New Orleans
nfl[nfl$teams == "Saints",]
## qbs teams ages cities
## 5 Brees Saints 41 New Orleans
nfl[nfl$ages > 20 & nfl$ages < 40,]
## qbs teams ages cities
## 1 Mahomes Chiefs 24 Kansas
## 2 Garoppolo 49ers 28 San Francisco
## 4 Rodgers Packers 36 Green Bay
nfl[nfl$ages > 30 & nfl$ages < 40, c("qbs", "ages")]
## qbs ages
## 4 Rodgers 36
mean(nfl$ages)
## [1] 34.2
sum(nfl$ages)
## [1] 171
length(nfl$ages)
## [1] 5
min(nfl$ages)
## [1] 24
max(nfl$ages)
## [1] 42
ifelse(ages < 25, "novato", "experto")
## [1] "novato" "experto" "experto" "experto" "experto"
m0 <- matrix(c(1:9), nrow = 3)
m0
## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9
colnames(m0) <- c("A", "B", "C")
m0
## A B C
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9
m0[, "B"]
## [1] 4 5 6
m0[m0[,"B"] > 4, c("A","C")]
## A C
## [1,] 2 8
## [2,] 3 9
data(mtcars)
head(mtcars)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
mean(mtcars$mpg) # media
## [1] 20.09062
median(mtcars$mpg) # mediana
## [1] 19.2
var(mtcars$mpg) # varianza
## [1] 36.3241
sd(mtcars$mpg) # desviación estándar
## [1] 6.026948
min(mtcars$mpg) # valor máximo
## [1] 10.4
max(mtcars$mpg) # valor mínimo
## [1] 33.9
mtcars[1,1] <- NA
mean(mtcars$mpg)
## [1] NA
median(mtcars$mpg)
## [1] NA
var(mtcars$mpg)
## [1] NA
sd(mtcars$mpg)
## [1] NA
min(mtcars$mpg)
## [1] NA
max(mtcars$mpg)
## [1] NA
mtcars[1,1] <- NA
mean(mtcars$mpg, na.rm = T)
## [1] 20.06129
median(mtcars$mpg, na.rm = T)
## [1] 19.2
var(mtcars$mpg, na.rm = T)
## [1] 37.50645
sd(mtcars$mpg, na.rm = T)
## [1] 6.124251
min(mtcars$mpg, na.rm = T)
## [1] 10.4
max(mtcars$mpg, na.rm = T)
## [1] 33.9
apply(mtcars, 2, mean)
## mpg cyl disp hp drat wt qsec
## NA 6.187500 230.721875 146.687500 3.596563 3.217250 17.848750
## vs am gear carb
## 0.437500 0.406250 3.687500 2.812500
apply(mtcars, 2, sd)
## mpg cyl disp hp drat wt
## NA 1.7859216 123.9386938 68.5628685 0.5346787 0.9784574
## qsec vs am gear carb
## 1.7869432 0.5040161 0.4989909 0.7378041 1.6152000
apply(mtcars, 2, mean, na.rm = T)
## mpg cyl disp hp drat wt qsec
## 20.061290 6.187500 230.721875 146.687500 3.596563 3.217250 17.848750
## vs am gear carb
## 0.437500 0.406250 3.687500 2.812500
apply(mtcars, 2, sd, na.rm = T)
## mpg cyl disp hp drat wt
## 6.1242511 1.7859216 123.9386938 68.5628685 0.5346787 0.9784574
## qsec vs am gear carb
## 1.7869432 0.5040161 0.4989909 0.7378041 1.6152000
cor(mtcars[, c("mpg", "hp", "wt", "cyl")])
## mpg hp wt cyl
## mpg 1 NA NA NA
## hp NA 1.0000000 0.6587479 0.8324475
## wt NA 0.6587479 1.0000000 0.7824958
## cyl NA 0.8324475 0.7824958 1.0000000
cov(mtcars[, c("mpg", "hp", "wt", "cyl")])
## mpg hp wt cyl
## mpg NA NA NA NA
## hp NA 4700.86694 44.192661 101.931452
## wt NA 44.19266 0.957379 1.367371
## cyl NA 101.93145 1.367371 3.189516
cor(mtcars[, c("mpg", "hp", "wt", "cyl")], use = "complete.obs")
## mpg hp wt cyl
## mpg 1.0000000 -0.7774885 -0.8703363 -0.8521139
## hp -0.7774885 1.0000000 0.6550588 0.8347180
## wt -0.8703363 0.6550588 1.0000000 0.7853924
## cyl -0.8521139 0.8347180 0.7853924 1.0000000
cov(mtcars[, c("mpg", "hp", "wt", "cyl")], use = "complete.obs")
## mpg hp wt cyl
## mpg 37.506452 -330.2752 -5.2685527 -9.472258
## hp -330.275161 4811.2495 44.9118022 105.092473
## wt -5.268553 44.9118 0.9770178 1.409097
## cyl -9.472258 105.0925 1.4090968 3.294624
library(corrplot)
## corrplot 0.92 loaded
corrplot(cor(mtcars[, c("mpg", "hp", "wt", "cyl")], use = "complete.obs"),
method = "color")
r es una matriz de 5 renglones
y 3 columnas. Supongamos que las columnas representan acciones
y que los renglones representan retornos diarios de 5 díasr <- matrix(runif(15), 5, 3)
r
## [,1] [,2] [,3]
## [1,] 0.8670258 0.69444787 0.1588098
## [2,] 0.2010973 0.63495154 0.3511251
## [3,] 0.1038993 0.83123855 0.8480530
## [4,] 0.2605629 0.31332815 0.5607126
## [5,] 0.2113934 0.01392533 0.2551474
w <- matrix(c(0.30, 0.40, 0.30), 3, 1)
w
## [,1]
## [1,] 0.3
## [2,] 0.4
## [3,] 0.3
r %*% w
## [,1]
## [1,] 0.5855298
## [2,] 0.4196474
## [3,] 0.6180811
## [4,] 0.3717139
## [5,] 0.1455324
m1 <- matrix(c(2,1,-2,3), 2, 2)
m1
## [,1] [,2]
## [1,] 2 -2
## [2,] 1 3
t(m1)
## [,1] [,2]
## [1,] 2 1
## [2,] -2 3
solve(m1)
## [,1] [,2]
## [1,] 0.375 0.25
## [2,] -0.125 0.25
m1 %*% solve(m1)
## [,1] [,2]
## [1,] 1 0
## [2,] 0 1
chol(m1)
## [,1] [,2]
## [1,] 1.414214 -1.414214
## [2,] 0.000000 1.000000
mi_fecha <- "2020-12-31"
mi_fecha
## [1] "2020-12-31"
class(mi_fecha)
## [1] "character"
#mi_fecha + 1
mi_fecha <- as.Date(mi_fecha, format = "%Y-%m-%d")
class(mi_fecha)
## [1] "Date"
mi_fecha
## [1] "2020-12-31"
mi_fecha + 1
## [1] "2021-01-01"
as.numeric(mi_fecha)
## [1] 18627
as.numeric(as.Date("1970-01-01"))
## [1] 0
as.Date("2020-12-31") - as.Date("1970-01-01")
## Time difference of 18627 days
seq(as.Date("2019-01-01"), as.Date("2020-12-31"), by = "quarter")
## [1] "2019-01-01" "2019-04-01" "2019-07-01" "2019-10-01" "2020-01-01"
## [6] "2020-04-01" "2020-07-01" "2020-10-01"
hoy <- Sys.Date()
class(hoy)
## [1] "Date"
ahora <- Sys.time()
ahora
## [1] "2022-10-23 01:00:45 -05"
class(ahora)
## [1] "POSIXct" "POSIXt"
as.numeric(ahora)
## [1] 1666504845
ahora + 1
## [1] "2022-10-23 01:00:46 -05"
gender <- c("Female", "Male", "Male", "Female", "Female", "Female", "Male")
genderf <- factor(gender)
genderf
## [1] Female Male Male Female Female Female Male
## Levels: Female Male
as.numeric(genderf)
## [1] 1 2 2 1 1 1 2
temps <- c("High", "High", "Med", "High", "Med", "Low", "Med")
tempsf <- factor(temps, ordered = T, levels = c("Low", "Med", "High"))
tempsf
## [1] High High Med High Med Low Med
## Levels: Low < Med < High
as.numeric(tempsf)
## [1] 3 3 2 3 2 1 2
tempst <- as.character(tempsf)
tempst
## [1] "High" "High" "Med" "High" "Med" "Low" "Med"
num.orig <- c(10, 9, 8, 11, 10, 8)
num.factor <- factor(num.orig)
num.factor
## [1] 10 9 8 11 10 8
## Levels: 8 9 10 11
num.numeric <- as.numeric(num.factor)
num.numeric
## [1] 3 2 1 4 3 1
num.text <- as.numeric(as.character(num.factor))
num.text
## [1] 10 9 8 11 10 8
tsla <- read.csv("TSLA.csv")
head(tsla)
## Date Open High Low Close Adj.Close Volume
## 1 2017-12-29 316.18 316.41 310.00 311.35 311.35 3777200
## 2 2018-01-02 312.00 322.11 311.00 320.53 320.53 4352200
## 3 2018-01-03 321.00 325.25 315.55 317.25 317.25 4521500
## 4 2018-01-04 312.87 318.55 305.68 314.62 314.62 9946300
## 5 2018-01-05 316.62 317.24 312.00 316.58 316.58 4591200
## 6 2018-01-08 316.00 337.02 315.50 336.41 336.41 9859400
tsla <- read.csv("TSLA.csv", skip = 0, header = T)
head(tsla)
## Date Open High Low Close Adj.Close Volume
## 1 2017-12-29 316.18 316.41 310.00 311.35 311.35 3777200
## 2 2018-01-02 312.00 322.11 311.00 320.53 320.53 4352200
## 3 2018-01-03 321.00 325.25 315.55 317.25 317.25 4521500
## 4 2018-01-04 312.87 318.55 305.68 314.62 314.62 9946300
## 5 2018-01-05 316.62 317.24 312.00 316.58 316.58 4591200
## 6 2018-01-08 316.00 337.02 315.50 336.41 336.41 9859400
summary(tsla)
## Date Open High Low
## Length:314 Min. :252.8 Min. :260.3 Min. :244.6
## Class :character 1st Qu.:293.1 1st Qu.:298.6 1st Qu.:288.0
## Mode :character Median :312.9 Median :318.5 Median :306.0
## Mean :313.9 Mean :320.1 Mean :307.5
## 3rd Qu.:337.9 3rd Qu.:345.8 3rd Qu.:331.9
## Max. :375.0 Max. :387.5 Max. :367.1
## Close Adj.Close Volume
## Min. :250.6 Min. :250.6 Min. : 3080700
## 1st Qu.:292.3 1st Qu.:292.3 1st Qu.: 5632475
## Median :312.5 Median :312.5 Median : 7203800
## Mean :314.1 Mean :314.1 Mean : 8571240
## 3rd Qu.:337.9 3rd Qu.:337.9 3rd Qu.: 9507275
## Max. :379.6 Max. :379.6 Max. :33649700
str(tsla)
## 'data.frame': 314 obs. of 7 variables:
## $ Date : chr "2017-12-29" "2018-01-02" "2018-01-03" "2018-01-04" ...
## $ Open : num 316 312 321 313 317 ...
## $ High : num 316 322 325 319 317 ...
## $ Low : num 310 311 316 306 312 ...
## $ Close : num 311 321 317 315 317 ...
## $ Adj.Close: num 311 321 317 315 317 ...
## $ Volume : int 3777200 4352200 4521500 9946300 4591200 9859400 7146600 4309900 6645500 4825100 ...
tsla$Date <- as.Date(tsla$Date, format = "%Y-%m-%d")
str(tsla)
## 'data.frame': 314 obs. of 7 variables:
## $ Date : Date, format: "2017-12-29" "2018-01-02" ...
## $ Open : num 316 312 321 313 317 ...
## $ High : num 316 322 325 319 317 ...
## $ Low : num 310 311 316 306 312 ...
## $ Close : num 311 321 317 315 317 ...
## $ Adj.Close: num 311 321 317 315 317 ...
## $ Volume : int 3777200 4352200 4521500 9946300 4591200 9859400 7146600 4309900 6645500 4825100 ...
summary(tsla)
## Date Open High Low
## Min. :2017-12-29 Min. :252.8 Min. :260.3 Min. :244.6
## 1st Qu.:2018-04-24 1st Qu.:293.1 1st Qu.:298.6 1st Qu.:288.0
## Median :2018-08-14 Median :312.9 Median :318.5 Median :306.0
## Mean :2018-08-15 Mean :313.9 Mean :320.1 Mean :307.5
## 3rd Qu.:2018-12-05 3rd Qu.:337.9 3rd Qu.:345.8 3rd Qu.:331.9
## Max. :2019-04-01 Max. :375.0 Max. :387.5 Max. :367.1
## Close Adj.Close Volume
## Min. :250.6 Min. :250.6 Min. : 3080700
## 1st Qu.:292.3 1st Qu.:292.3 1st Qu.: 5632475
## Median :312.5 Median :312.5 Median : 7203800
## Mean :314.1 Mean :314.1 Mean : 8571240
## 3rd Qu.:337.9 3rd Qu.:337.9 3rd Qu.: 9507275
## Max. :379.6 Max. :379.6 Max. :33649700
plot(x = tsla$Date, y = tsla$Adj.Close, type = "l", col = "red")
barplot(tsla$Adj.Close)
hist(tsla$Adj.Close)
boxplot(tsla$Adj.Close)
library(ggplot2)
ggplot(data = tsla, aes(x = Date, y = Adj.Close)) + geom_line(color = "red")
ggplot(data = tsla, aes(x = Date, y = Adj.Close)) + geom_col()
ggplot(data = tsla, aes(x = Adj.Close)) + geom_histogram(color = "orange", fill = "gold")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
ggplot(data = tsla, aes(y = Adj.Close)) + geom_boxplot(color = "blue", fill = "lightblue")
\[ logreturn{_i}=\ln (\frac{adj.close_i}{adj.close_{i-1}}) \]
tsla$LaggedAdj.Close <- dplyr::lag(tsla$Adj.Close)
head(tsla)
## Date Open High Low Close Adj.Close Volume LaggedAdj.Close
## 1 2017-12-29 316.18 316.41 310.00 311.35 311.35 3777200 NA
## 2 2018-01-02 312.00 322.11 311.00 320.53 320.53 4352200 311.35
## 3 2018-01-03 321.00 325.25 315.55 317.25 317.25 4521500 320.53
## 4 2018-01-04 312.87 318.55 305.68 314.62 314.62 9946300 317.25
## 5 2018-01-05 316.62 317.24 312.00 316.58 316.58 4591200 314.62
## 6 2018-01-08 316.00 337.02 315.50 336.41 336.41 9859400 316.58
tsla$LogReturns <- log(tsla$Adj.Close/dplyr::lag(tsla$Adj.Close))
head(tsla)
## Date Open High Low Close Adj.Close Volume LaggedAdj.Close
## 1 2017-12-29 316.18 316.41 310.00 311.35 311.35 3777200 NA
## 2 2018-01-02 312.00 322.11 311.00 320.53 320.53 4352200 311.35
## 3 2018-01-03 321.00 325.25 315.55 317.25 317.25 4521500 320.53
## 4 2018-01-04 312.87 318.55 305.68 314.62 314.62 9946300 317.25
## 5 2018-01-05 316.62 317.24 312.00 316.58 316.58 4591200 314.62
## 6 2018-01-08 316.00 337.02 315.50 336.41 336.41 9859400 316.58
## LogReturns
## 1 NA
## 2 0.029058172
## 3 -0.010285766
## 4 -0.008324561
## 5 0.006210388
## 6 0.060754733
tsla$yrMonth <- substr(tsla$Date, 1, 7)
head(tsla)
## Date Open High Low Close Adj.Close Volume LaggedAdj.Close
## 1 2017-12-29 316.18 316.41 310.00 311.35 311.35 3777200 NA
## 2 2018-01-02 312.00 322.11 311.00 320.53 320.53 4352200 311.35
## 3 2018-01-03 321.00 325.25 315.55 317.25 317.25 4521500 320.53
## 4 2018-01-04 312.87 318.55 305.68 314.62 314.62 9946300 317.25
## 5 2018-01-05 316.62 317.24 312.00 316.58 316.58 4591200 314.62
## 6 2018-01-08 316.00 337.02 315.50 336.41 336.41 9859400 316.58
## LogReturns yrMonth
## 1 NA 2017-12
## 2 0.029058172 2018-01
## 3 -0.010285766 2018-01
## 4 -0.008324561 2018-01
## 5 0.006210388 2018-01
## 6 0.060754733 2018-01
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
tsla %>% filter(Volume <= 4000000 | Volume >= 30000000) %>%
select(Date, Adj.Close, Volume)
## Date Adj.Close Volume
## 1 2017-12-29 311.35 3777200
## 2 2018-02-02 343.75 3704800
## 3 2018-02-14 322.31 3950700
## 4 2018-02-21 333.30 3219600
## 5 2018-03-05 333.35 3823800
## 6 2018-03-08 329.10 3566200
## 7 2018-05-25 278.85 3875100
## 8 2018-08-07 379.57 30875800
## 9 2018-08-24 322.82 3602600
## 10 2018-09-28 264.77 33649700
## 11 2018-11-29 341.17 3080700
## 12 2019-01-17 347.31 3676700
## 13 2019-02-15 307.88 3904900
tsla %>% filter(Date >= "2018-12-31" & Date <= "2019-01-10") %>%
select(Date, Adj.Close, Volume)
## Date Adj.Close Volume
## 1 2018-12-31 332.80 6302300
## 2 2019-01-02 310.12 11658600
## 3 2019-01-03 300.36 6965200
## 4 2019-01-04 317.69 7394100
## 5 2019-01-07 334.96 7551200
## 6 2019-01-08 335.35 7008500
## 7 2019-01-09 338.53 5432900
## 8 2019-01-10 344.97 6056400
tsla %>% group_by(yrMonth) %>%
select(Date, Adj.Close, Volume) %>%
summarize(obs = n(), meanAdjClose = mean(Adj.Close), meanVolume = mean(Volume))
## Adding missing grouping variables: `yrMonth`
## # A tibble: 17 × 4
## yrMonth obs meanAdjClose meanVolume
## <chr> <int> <dbl> <dbl>
## 1 2017-12 1 311. 3777200
## 2 2018-01 21 339. 5916748.
## 3 2018-02 19 336. 5746842.
## 4 2018-03 21 316. 7488976.
## 5 2018-04 21 290. 9062419.
## 6 2018-05 22 290. 7070277.
## 7 2018-06 21 336. 10163100
## 8 2018-07 21 312. 8206105.
## 9 2018-08 23 331. 12055061.
## 10 2018-09 19 290. 10319758.
## 11 2018-10 23 285. 12450465.
## 12 2018-11 21 344. 6334243.
## 13 2018-12 19 344. 7707958.
## 14 2019-01 21 318. 8364386.
## 15 2019-02 19 308. 6765795.
## 16 2019-03 21 278. 10180643.
## 17 2019-04 1 289. 8103300
names(tsla)
## [1] "Date" "Open" "High" "Low"
## [5] "Close" "Adj.Close" "Volume" "LaggedAdj.Close"
## [9] "LogReturns" "yrMonth"
names(tsla) <- c("Date", "O", "H", "L", "C", "Adj.Close", "V")
head(tsla)
## Date O H L C Adj.Close V NA NA
## 1 2017-12-29 316.18 316.41 310.00 311.35 311.35 3777200 NA NA
## 2 2018-01-02 312.00 322.11 311.00 320.53 320.53 4352200 311.35 0.029058172
## 3 2018-01-03 321.00 325.25 315.55 317.25 317.25 4521500 320.53 -0.010285766
## 4 2018-01-04 312.87 318.55 305.68 314.62 314.62 9946300 317.25 -0.008324561
## 5 2018-01-05 316.62 317.24 312.00 316.58 316.58 4591200 314.62 0.006210388
## 6 2018-01-08 316.00 337.02 315.50 336.41 336.41 9859400 316.58 0.060754733
## NA
## 1 2017-12
## 2 2018-01
## 3 2018-01
## 4 2018-01
## 5 2018-01
## 6 2018-01
tsla <- tsla[, c("Date", "Adj.Close")]
tsla <- tsla %>% select("Date", "Adj.Close")
tsla <- read.csv("TSLA.csv")[, c("Date", "Adj.Close")]
tsla$Date <- as.Date(tsla$Date)
plot(x = tsla$Date, y = tsla$Adj.Close,
xlab = "Fecha", ylab = "Precio ajustado de cierre",
type = "l", col = "red")
rm(list = ls()) # para limpiar los datos del ambiente global
archivos <- c("MCD.csv", "TSLA.csv", "WMT.csv")
consolidado <- read.csv(archivos[1])[, c("Date", "Adj.Close")]
head(consolidado)
## Date Adj.Close
## 1 2017-12-29 166.8067
## 2 2018-01-02 167.8727
## 3 2018-01-03 167.1653
## 4 2018-01-04 168.3379
## 5 2018-01-05 168.6771
## 6 2018-01-08 168.5608
temporal <- read.csv(archivos[2])[, c("Date", "Adj.Close")]
head(temporal)
## Date Adj.Close
## 1 2017-12-29 311.35
## 2 2018-01-02 320.53
## 3 2018-01-03 317.25
## 4 2018-01-04 314.62
## 5 2018-01-05 316.58
## 6 2018-01-08 336.41
tickers <- sub(".csv", "", archivos)
tickers
## [1] "MCD" "TSLA" "WMT"
# el [2] en names(consolidado)[2] se debe a que solo queremos renombrar la
# segunda columna, no la primera. Le pondremos como nombre el valor en la 1era
# posición de tickers (tickers[1]).
names(consolidado)[2] <- tickers[1]
head(consolidado)
## Date MCD
## 1 2017-12-29 166.8067
## 2 2018-01-02 167.8727
## 3 2018-01-03 167.1653
## 4 2018-01-04 168.3379
## 5 2018-01-05 168.6771
## 6 2018-01-08 168.5608
# el [2] en names(temporal)[2] se debe a que solo queremos renombrar la
# segunda columna, no la primera. Le pondremos como nombre el valor en la 2da
# posición de tickers (tickers[2]).
names(temporal)[2] <- tickers[2]
head(temporal)
## Date TSLA
## 1 2017-12-29 311.35
## 2 2018-01-02 320.53
## 3 2018-01-03 317.25
## 4 2018-01-04 314.62
## 5 2018-01-05 316.58
## 6 2018-01-08 336.41
consolidado <- merge(x = consolidado, y = temporal,
by.x = "Date", by.y = "Date",
all.x = T, all.y = T)
head(consolidado)
## Date MCD TSLA
## 1 2017-12-29 166.8067 311.35
## 2 2018-01-02 167.8727 320.53
## 3 2018-01-03 167.1653 317.25
## 4 2018-01-04 168.3379 314.62
## 5 2018-01-05 168.6771 316.58
## 6 2018-01-08 168.5608 336.41
# Aquí leemos la información de "WMT.csv". Nota como el único cambio respecto al
# bloque donde leímos la información de "TSLA.csv", es el subíndice del vector
# "archivos": antes era 2 y ahora es 3
# Aprovechamos para renombrar la columna "Adj.Close" de una vez, de nuevo
# observa como ahora tomamos la posición 3 del vector "tickers".
temporal <- read.csv(archivos[3])[, c("Date", "Adj.Close")]
names(temporal)[2] <- tickers[3]
head(temporal)
## Date WMT
## 1 2017-12-29 95.94424
## 2 2018-01-02 95.78879
## 3 2018-01-03 96.62435
## 4 2018-01-04 96.71180
## 5 2018-01-05 97.28503
## 6 2018-01-08 98.72298
consolidado <- merge(x = consolidado, y = temporal,
by.x = "Date", by.y = "Date",
all.x = T, all.y = T)
head(consolidado)
## Date MCD TSLA WMT
## 1 2017-12-29 166.8067 311.35 95.94424
## 2 2018-01-02 167.8727 320.53 95.78879
## 3 2018-01-03 167.1653 317.25 96.62435
## 4 2018-01-04 168.3379 314.62 96.71180
## 5 2018-01-05 168.6771 316.58 97.28503
## 6 2018-01-08 168.5608 336.41 98.72298
# Esta función toma por default el "Working Directory", así que si ya definiste
# la carpeta con los archivos que quieres leer como el directorio de trabajo,
# puedes utilizar la función sin especificar el argumento "path". De lo
# contrario, habría que especificar la ruta de la carpeta ahí.
archivos <- list.files(pattern = "(*).csv", recursive = FALSE)
archivos
## [1] "MCD.csv" "TSLA.csv" "WMT.csv"
# quitar la extensión .csv para tener solo los nombres
tickers <- sub(".csv", "", archivos)
tickers
## [1] "MCD" "TSLA" "WMT"
# para tener un conteo de los archivos en la lista y saber cuántas iteraciones
# tendremos que hacer en su momento
n <- length(archivos)
n
## [1] 3
# R base no tiene funciones para leer archivos de Excel, pero hay muchos
# paquetes que pueden agregar esa funcionalidad. openxlsx es solo uno de varias
# opciones buenas
library(openxlsx)
netflix <- read.xlsx("https://public.tableau.com/s/sites/default/files/media/netflix_titles.xlsx")
head(netflix)
## duration_minutes duration_seasons type
## 1 90 NA Movie
## 2 94 NA Movie
## 3 <NA> 1 TV Show
## 4 <NA> 1 TV Show
## 5 99 NA Movie
## 6 <NA> 1 TV Show
## title date_added release_year rating
## 1 Norm of the North: King Sized Adventure 43717 2019 TV-PG
## 2 Jandino: Whatever it Takes 42622 2016 TV-MA
## 3 Transformers Prime 43351 2013 TV-Y7-FV
## 4 Transformers: Robots in Disguise 43351 2016 TV-Y7
## 5 #realityhigh 42986 2017 TV-14
## 6 Apaches 42986 2016 TV-MA
## description
## 1 Before planning an awesome wedding for his grandfather, a polar bear king must take back a stolen artifact from an evil archaeologist first.
## 2 Jandino Asporaat riffs on the challenges of raising kids and serenades the audience with a rousing rendition of "Sex on Fire" in his comedy show.
## 3 With the help of three human allies, the Autobots once again protect Earth from the onslaught of the Decepticons and their leader, Megatron.
## 4 When a prison ship crash unleashes hundreds of Decepticons on Earth, Bumblebee leads a new Autobot force to protect humankind.
## 5 When nerdy high schooler Dani finally attracts the interest of her longtime crush, she lands in the cross hairs of his ex, a social media celebrity.
## 6 A young journalist is forced into a life of crime to save his father and family in this series based on the novel by Miguel Sáez Carral.
## show_id
## 1 81145628
## 2 80117401
## 3 70234439
## 4 80058654
## 5 80125979
## 6 80163890
rm(list = ls()) # para limpiar los datos del ambiente global
library(quantmod) # se requiere activar quantmod en cada sesión que se utilice
## Loading required package: xts
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
##
## Attaching package: 'xts'
## The following objects are masked from 'package:dplyr':
##
## first, last
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
gdp <- getSymbols(Symbols = "GDP", src = "FRED", auto.assign = F)
## 'getSymbols' currently uses auto.assign=TRUE by default, but will
## use auto.assign=FALSE in 0.5-0. You will still be able to use
## 'loadSymbols' to automatically load data. getOption("getSymbols.env")
## and getOption("getSymbols.auto.assign") will still be checked for
## alternate defaults.
##
## This message is shown once per session and may be disabled by setting
## options("getSymbols.warning4.0"=FALSE). See ?getSymbols for details.
head(gdp)
## GDP
## 1947-01-01 243.164
## 1947-04-01 245.968
## 1947-07-01 249.585
## 1947-10-01 259.745
## 1948-01-01 265.742
## 1948-04-01 272.567
usdmxn <- getSymbols("USD/MXN", src = "oanda", auto.assign = F)
head(usdmxn)
## USD.MXN
## 2022-04-27 20.39800
## 2022-04-28 20.45723
## 2022-04-29 20.38567
## 2022-04-30 20.42520
## 2022-05-01 20.42515
## 2022-05-02 20.43713
mcd <- getSymbols(Symbols = "mcd", src = "yahoo",
from = "2020-06-30", to = "2021-06-30",
periodicity = "daily", auto.assign = F)
head(mcd)
## MCD.Open MCD.High MCD.Low MCD.Close MCD.Volume MCD.Adjusted
## 2020-06-30 182.92 185.20 181.89 184.47 3163100 175.3101
## 2020-07-01 184.95 186.44 183.72 184.66 2193800 175.4907
## 2020-07-02 187.00 187.00 182.86 183.52 2690200 174.4073
## 2020-07-06 186.00 188.72 184.14 188.50 3171500 179.1400
## 2020-07-07 187.37 187.85 185.25 185.82 2399600 176.5931
## 2020-07-08 185.50 187.23 184.75 185.85 2776400 176.6216
class(mcd)
## [1] "xts" "zoo"
str(mcd)
## An 'xts' object on 2020-06-30/2021-06-29 containing:
## Data: num [1:252, 1:6] 183 185 187 186 187 ...
## - attr(*, "dimnames")=List of 2
## ..$ : NULL
## ..$ : chr [1:6] "MCD.Open" "MCD.High" "MCD.Low" "MCD.Close" ...
## Indexed by objects of class: [Date] TZ: UTC
## xts Attributes:
## List of 2
## $ src : chr "yahoo"
## $ updated: POSIXct[1:1], format: "2022-10-23 01:00:57"
head(mcd, 5)
## MCD.Open MCD.High MCD.Low MCD.Close MCD.Volume MCD.Adjusted
## 2020-06-30 182.92 185.20 181.89 184.47 3163100 175.3101
## 2020-07-01 184.95 186.44 183.72 184.66 2193800 175.4907
## 2020-07-02 187.00 187.00 182.86 183.52 2690200 174.4073
## 2020-07-06 186.00 188.72 184.14 188.50 3171500 179.1400
## 2020-07-07 187.37 187.85 185.25 185.82 2399600 176.5931
tail(mcd, 3)
## MCD.Open MCD.High MCD.Low MCD.Close MCD.Volume MCD.Adjusted
## 2021-06-25 232.74 233.41 232.34 232.42 3000100 226.1089
## 2021-06-28 232.67 232.75 230.01 231.09 2901700 224.8150
## 2021-06-29 231.53 231.71 230.05 230.37 1932100 224.1146
first(mcd, "2 weeks")
## MCD.Open MCD.High MCD.Low MCD.Close MCD.Volume MCD.Adjusted
## 2020-06-30 182.92 185.20 181.89 184.47 3163100 175.3101
## 2020-07-01 184.95 186.44 183.72 184.66 2193800 175.4907
## 2020-07-02 187.00 187.00 182.86 183.52 2690200 174.4073
## 2020-07-06 186.00 188.72 184.14 188.50 3171500 179.1400
## 2020-07-07 187.37 187.85 185.25 185.82 2399600 176.5931
## 2020-07-08 185.50 187.23 184.75 185.85 2776400 176.6216
## 2020-07-09 186.22 186.59 182.62 184.33 2333500 175.1770
## 2020-07-10 183.78 185.44 183.01 184.88 2714100 175.6998
last(mcd, "1 month")
## MCD.Open MCD.High MCD.Low MCD.Close MCD.Volume MCD.Adjusted
## 2021-06-01 235.98 235.99 232.74 233.24 2574300 226.9067
## 2021-06-02 233.97 234.33 232.81 233.78 3172000 227.4320
## 2021-06-03 232.57 232.76 230.15 232.45 3249400 226.1381
## 2021-06-04 233.44 233.80 232.07 233.38 1615200 227.0428
## 2021-06-07 234.00 234.07 231.16 231.69 1877000 225.3987
## 2021-06-08 231.50 233.98 231.34 232.64 2106200 226.3230
## 2021-06-09 232.98 234.32 231.45 231.47 1982200 225.1847
## 2021-06-10 232.05 234.90 231.93 234.59 2534000 228.2200
## 2021-06-11 235.00 237.50 234.71 236.93 2654300 230.4965
## 2021-06-14 237.18 237.77 234.81 236.98 1836800 230.5451
## 2021-06-15 237.53 237.81 235.66 236.35 1948300 229.9322
## 2021-06-16 237.21 237.28 233.78 235.58 2951700 229.1831
## 2021-06-17 235.08 236.27 233.28 233.88 1895600 227.5293
## 2021-06-18 231.47 232.89 228.82 229.62 4408200 223.3849
## 2021-06-21 230.63 233.23 229.47 232.90 2193000 226.5759
## 2021-06-22 233.49 234.86 232.43 233.88 1758300 227.5293
## 2021-06-23 233.30 234.45 232.70 233.24 1701500 226.9067
## 2021-06-24 234.10 235.16 232.74 233.33 1839800 226.9942
## 2021-06-25 232.74 233.41 232.34 232.42 3000100 226.1089
## 2021-06-28 232.67 232.75 230.01 231.09 2901700 224.8150
## 2021-06-29 231.53 231.71 230.05 230.37 1932100 224.1146
chartSeries(mcd,
subset = "last 8 weeks",
type = "candle",
TA = c(addVo(), addSMA()))
mcd.df <- data.frame(fecha = index(mcd), coredata(mcd))
head(mcd.df)
## fecha MCD.Open MCD.High MCD.Low MCD.Close MCD.Volume MCD.Adjusted
## 1 2020-06-30 182.92 185.20 181.89 184.47 3163100 175.3101
## 2 2020-07-01 184.95 186.44 183.72 184.66 2193800 175.4907
## 3 2020-07-02 187.00 187.00 182.86 183.52 2690200 174.4073
## 4 2020-07-06 186.00 188.72 184.14 188.50 3171500 179.1400
## 5 2020-07-07 187.37 187.85 185.25 185.82 2399600 176.5931
## 6 2020-07-08 185.50 187.23 184.75 185.85 2776400 176.6216
class(mcd.df)
## [1] "data.frame"
str(mcd.df)
## 'data.frame': 252 obs. of 7 variables:
## $ fecha : Date, format: "2020-06-30" "2020-07-01" ...
## $ MCD.Open : num 183 185 187 186 187 ...
## $ MCD.High : num 185 186 187 189 188 ...
## $ MCD.Low : num 182 184 183 184 185 ...
## $ MCD.Close : num 184 185 184 188 186 ...
## $ MCD.Volume : num 3163100 2193800 2690200 3171500 2399600 ...
## $ MCD.Adjusted: num 175 175 174 179 177 ...