Skip to content

hist from a list

5 messages · Pedro páramo, Michael Dewey, Rui Barradas +2 more

#
Hi all,

I attach my code, the think is I want to make a bar plot the last variable
called "bwchist"

 so the X axis are "Accion" and the y axis are "reval" values.

I have prove class(bwchist) and says dataframe but its still a list because
it says me

I have prove to unlist,  but it doesnt work

hist(bwchist)
Error in hist.default(bwchist) : 'x' must be numeric

Or

barplot(bwchist)
Error in barplot.default(bwchist) : 'height' must be a vector or a matrix

library(PerformanceAnalytics)
library(dplyr)
library(tibble)
library(lubridate)
library(PerformanceAnalytics)
library(quantmod)
library(ggplot2)
library(png)
library(grid)
library(RCurl)
library(tidyquant)
library(timetk)
library(data.table)



Acciona<- tq_get("ANA.MC",from = '2019-12-31',get = "stock.prices")
ACS<- tq_get("ACS.MC",from = '2019-12-31',get = "stock.prices")
Aena<- tq_get("AENA.MC",from = '2019-12-31',get = "stock.prices")
Amadeus<- tq_get("AMS.MC",from = '2019-12-31',get = "stock.prices")
ArcelorMittal<- tq_get("MTS.MC",from = '2019-12-31',get = "stock.prices")
BBVA<- tq_get("BBVA.MC",from = '2019-12-31',get = "stock.prices")
Sabadell<- tq_get("SAB.MC",from = '2019-12-31',get = "stock.prices")
Santander<- tq_get("SAN.MC",from = '2019-12-31',get = "stock.prices")
Bankinter<- tq_get("BKT.MC",from = '2019-12-31',get = "stock.prices")
CaixaBank<- tq_get("CABK.MC",from = '2019-12-31',get = "stock.prices")
Cellnex<- tq_get("CLNX.MC",from = '2019-12-31',get = "stock.prices")
Enagas<- tq_get("ENG.MC",from = '2019-12-31',get = "stock.prices")
ENCE<- tq_get("ENC.MC",from = '2019-12-31',get = "stock.prices")
Endesa<- tq_get("ELE.MC",from = '2019-12-31',get = "stock.prices")
Ferrovial<- tq_get("FER.MC",from = '2019-12-31',get = "stock.prices")
Grifols<- tq_get("GRF.MC",from = '2019-12-31',get = "stock.prices")
Iberdrola<- tq_get("IBE.MC",from = '2019-12-31',get = "stock.prices")
Inditex<- tq_get("ITX.MC",from = '2019-12-31',get = "stock.prices")
Colonial<- tq_get("COL.MC",from = '2019-12-31',get = "stock.prices")
IAG<- tq_get("IAG.MC",from = '2019-12-31',get = "stock.prices")
Mapfre<- tq_get("MAP.MC",from = '2019-12-31',get = "stock.prices")
Melia<- tq_get("MEL.MC",from = '2019-12-31',get = "stock.prices")
Merlin<- tq_get("MRL.MC",from = '2019-12-31',get = "stock.prices")
Naturgy<- tq_get("NTGY.MC",from = '2019-12-31',get = "stock.prices")
REE<- tq_get("REE.MC",from = '2019-12-31',get = "stock.prices")
Repsol<- tq_get("REP.MC",from = '2019-12-31',get = "stock.prices")
SGamesa<- tq_get("SGRE.MC",from = '2019-12-31',get = "stock.prices")
Telefonica<- tq_get("TEF.MC",from = '2019-12-31',get = "stock.prices")
Viscofan<- tq_get("VIS.MC",from = '2019-12-31',get = "stock.prices")
Acerinox<- tq_get("ACX.MC",from = '2019-12-31',get = "stock.prices")
Bankia<- tq_get("BKIA.MC",from = '2019-12-31',get = "stock.prices")
CIE<- tq_get("CIE.MC",from = '2019-12-31',get = "stock.prices")
MasMovil<- tq_get("MAS.MC",from = '2019-12-31',get = "stock.prices")
Almirall<- tq_get("ALM.MC",from = '2019-12-31',get = "stock.prices")
Indra<- tq_get("IDR.MC",from ='2019-12-31',get = "stock.prices")

Indra_daily_returns <- Indra %>%
  tq_transmute(select = adjusted,           # this specifies which column
to select
               mutate_fun = periodReturn,   # This specifies what to do
with that column
               period = "daily",      # This argument calculates Daily
returns
               col_rename = "idr_returns") # renames the column
Indra_cum_returns <- Indra_daily_returns %>%
  mutate(cr = cumprod(1 + idr_returns)) %>%      # using the cumprod
function
  mutate(cumulative_returns = cr - 1)

Almirall_daily_returns <- Almirall %>%
  tq_transmute(select = adjusted,           # this specifies which column
to select
               mutate_fun = periodReturn,   # This specifies what to do
with that column
               period = "daily",      # This argument calculates Daily
returns
               col_rename = "idr_returns") # renames the column
Almirall_cum_returns <- Almirall_daily_returns %>%
  mutate(cr = cumprod(1 + idr_returns)) %>%      # using the cumprod
function
  mutate(cumulative_returns = cr - 1)

Acciona_daily_returns <- Acciona %>%
  tq_transmute(select = adjusted,           # this specifies which column
to select
               mutate_fun = periodReturn,   # This specifies what to do
with that column
               period = "daily",      # This argument calculates Daily
returns
               col_rename = "idr_returns") # renames the column
Acciona_cum_returns <- Acciona_daily_returns %>%
  mutate(cr = cumprod(1 + idr_returns)) %>%      # using the cumprod
function
  mutate(cumulative_returns = cr - 1)

ACS_daily_returns <- ACS %>%
  tq_transmute(select = adjusted,           # this specifies which column
to select
               mutate_fun = periodReturn,   # This specifies what to do
with that column
               period = "daily",      # This argument calculates Daily
returns
               col_rename = "idr_returns") # renames the column
ACS_cum_returns <- ACS_daily_returns %>%
  mutate(cr = cumprod(1 + idr_returns)) %>%      # using the cumprod
function
  mutate(cumulative_returns = cr - 1)

Aena_daily_returns <- Aena %>%
  tq_transmute(select = adjusted,           # this specifies which column
to select
               mutate_fun = periodReturn,   # This specifies what to do
with that column
               period = "daily",      # This argument calculates Daily
returns
               col_rename = "idr_returns") # renames the column
Aena_cum_returns <- Aena_daily_returns %>%
  mutate(cr = cumprod(1 + idr_returns)) %>%      # using the cumprod
function
  mutate(cumulative_returns = cr - 1)

Amadeus_daily_returns <- Amadeus %>%
  tq_transmute(select = adjusted,           # this specifies which column
to select
               mutate_fun = periodReturn,   # This specifies what to do
with that column
               period = "daily",      # This argument calculates Daily
returns
               col_rename = "idr_returns") # renames the column
Amadeus_cum_returns <- Amadeus_daily_returns %>%
  mutate(cr = cumprod(1 + idr_returns)) %>%      # using the cumprod
function
  mutate(cumulative_returns = cr - 1)

ArcelorMittal_daily_returns <- ArcelorMittal %>%
  tq_transmute(select = adjusted,           # this specifies which column
to select
               mutate_fun = periodReturn,   # This specifies what to do
with that column
               period = "daily",      # This argument calculates Daily
returns
               col_rename = "idr_returns") # renames the column
ArcelorMittal_cum_returns <- ArcelorMittal_daily_returns %>%
  mutate(cr = cumprod(1 + idr_returns)) %>%      # using the cumprod
function
  mutate(cumulative_returns = cr - 1)

BBVA_daily_returns <- BBVA %>%
  tq_transmute(select = adjusted,           # this specifies which column
to select
               mutate_fun = periodReturn,   # This specifies what to do
with that column
               period = "daily",      # This argument calculates Daily
returns
               col_rename = "idr_returns") # renames the column
BBVA_cum_returns <- BBVA_daily_returns %>%
  mutate(cr = cumprod(1 + idr_returns)) %>%      # using the cumprod
function
  mutate(cumulative_returns = cr - 1)


Sabadell_daily_returns <- Sabadell %>%
  tq_transmute(select = adjusted,           # this specifies which column
to select
               mutate_fun = periodReturn,   # This specifies what to do
with that column
               period = "daily",      # This argument calculates Daily
returns
               col_rename = "idr_returns") # renames the column
Sabadell_cum_returns <- Sabadell_daily_returns %>%
  mutate(cr = cumprod(1 + idr_returns)) %>%      # using the cumprod
function
  mutate(cumulative_returns = cr - 1)

Santander_daily_returns <- Santander %>%
  tq_transmute(select = adjusted,           # this specifies which column
to select
               mutate_fun = periodReturn,   # This specifies what to do
with that column
               period = "daily",      # This argument calculates Daily
returns
               col_rename = "idr_returns") # renames the column
Santander_cum_returns <- Santander_daily_returns %>%
  mutate(cr = cumprod(1 + idr_returns)) %>%      # using the cumprod
function
  mutate(cumulative_returns = cr - 1)


Bankinter_daily_returns <- Bankinter %>%
  tq_transmute(select = adjusted,           # this specifies which column
to select
               mutate_fun = periodReturn,   # This specifies what to do
with that column
               period = "daily",      # This argument calculates Daily
returns
               col_rename = "idr_returns") # renames the column
Bankinter_cum_returns <- Bankinter_daily_returns %>%
  mutate(cr = cumprod(1 + idr_returns)) %>%      # using the cumprod
function
  mutate(cumulative_returns = cr - 1)

CaixaBank_daily_returns <- CaixaBank %>%
  tq_transmute(select = adjusted,           # this specifies which column
to select
               mutate_fun = periodReturn,   # This specifies what to do
with that column
               period = "daily",      # This argument calculates Daily
returns
               col_rename = "idr_returns") # renames the column
CaixaBank_cum_returns <- CaixaBank_daily_returns %>%
  mutate(cr = cumprod(1 + idr_returns)) %>%      # using the cumprod
function
  mutate(cumulative_returns = cr - 1)

Cellnex_daily_returns <- Cellnex %>%
  tq_transmute(select = adjusted,           # this specifies which column
to select
               mutate_fun = periodReturn,   # This specifies what to do
with that column
               period = "daily",      # This argument calculates Daily
returns
               col_rename = "idr_returns") # renames the column
Cellnex_cum_returns <- Cellnex_daily_returns %>%
  mutate(cr = cumprod(1 + idr_returns)) %>%      # using the cumprod
function
  mutate(cumulative_returns = cr - 1)

CIE_daily_returns <- CIE %>%
  tq_transmute(select = adjusted,           # this specifies which column
to select
               mutate_fun = periodReturn,   # This specifies what to do
with that column
               period = "daily",      # This argument calculates Daily
returns
               col_rename = "idr_returns") # renames the column
CIE_cum_returns <- CIE_daily_returns %>%
  mutate(cr = cumprod(1 + idr_returns)) %>%      # using the cumprod
function
  mutate(cumulative_returns = cr - 1)

CIE_daily_returns <- CIE %>%
  tq_transmute(select = adjusted,           # this specifies which column
to select
               mutate_fun = periodReturn,   # This specifies what to do
with that column
               period = "daily",      # This argument calculates Daily
returns
               col_rename = "idr_returns") # renames the column
CIE_cum_returns <- CIE_daily_returns %>%
  mutate(cr = cumprod(1 + idr_returns)) %>%      # using the cumprod
function
  mutate(cumulative_returns = cr - 1)

Enagas_daily_returns <- Enagas %>%
  tq_transmute(select = adjusted,           # this specifies which column
to select
               mutate_fun = periodReturn,   # This specifies what to do
with that column
               period = "daily",      # This argument calculates Daily
returns
               col_rename = "idr_returns") # renames the column
Enagas_cum_returns <- Enagas_daily_returns %>%
  mutate(cr = cumprod(1 + idr_returns)) %>%      # using the cumprod
function
  mutate(cumulative_returns = cr - 1)


ENCE_daily_returns <- ENCE %>%
  tq_transmute(select = adjusted,           # this specifies which column
to select
               mutate_fun = periodReturn,   # This specifies what to do
with that column
               period = "daily",      # This argument calculates Daily
returns
               col_rename = "idr_returns") # renames the column
ENCE_cum_returns <- ENCE_daily_returns %>%
  mutate(cr = cumprod(1 + idr_returns)) %>%      # using the cumprod
function
  mutate(cumulative_returns = cr - 1)
Endesa_daily_returns <- Endesa %>%
  tq_transmute(select = adjusted,           # this specifies which column
to select
               mutate_fun = periodReturn,   # This specifies what to do
with that column
               period = "daily",      # This argument calculates Daily
returns
               col_rename = "idr_returns") # renames the column
Endesa_cum_returns <- Endesa_daily_returns %>%
  mutate(cr = cumprod(1 + idr_returns)) %>%      # using the cumprod
function
  mutate(cumulative_returns = cr - 1)
Ferrovial_daily_returns <- Ferrovial %>%
  tq_transmute(select = adjusted,           # this specifies which column
to select
               mutate_fun = periodReturn,   # This specifies what to do
with that column
               period = "daily",      # This argument calculates Daily
returns
               col_rename = "idr_returns") # renames the column
Ferrovial_cum_returns <- Ferrovial_daily_returns %>%
  mutate(cr = cumprod(1 + idr_returns)) %>%      # using the cumprod
function
  mutate(cumulative_returns = cr - 1)

Grifols_daily_returns <- Grifols %>%
  tq_transmute(select = adjusted,           # this specifies which column
to select
               mutate_fun = periodReturn,   # This specifies what to do
with that column
               period = "daily",      # This argument calculates Daily
returns
               col_rename = "idr_returns") # renames the column
Grifols_cum_returns <- Grifols_daily_returns %>%
  mutate(cr = cumprod(1 + idr_returns)) %>%      # using the cumprod
function
  mutate(cumulative_returns = cr - 1)

Iberdrola_daily_returns <- Iberdrola %>%
  tq_transmute(select = adjusted,           # this specifies which column
to select
               mutate_fun = periodReturn,   # This specifies what to do
with that column
               period = "daily",      # This argument calculates Daily
returns
               col_rename = "idr_returns") # renames the column
Iberdrola_cum_returns <- Iberdrola_daily_returns %>%
  mutate(cr = cumprod(1 + idr_returns)) %>%      # using the cumprod
function
  mutate(cumulative_returns = cr - 1)

Inditex_daily_returns <- Inditex %>%
  tq_transmute(select = adjusted,           # this specifies which column
to select
               mutate_fun = periodReturn,   # This specifies what to do
with that column
               period = "daily",      # This argument calculates Daily
returns
               col_rename = "idr_returns") # renames the column
Inditex_cum_returns <- Inditex_daily_returns %>%
  mutate(cr = cumprod(1 + idr_returns)) %>%      # using the cumprod
function
  mutate(cumulative_returns = cr - 1)

Colonial_daily_returns <- Colonial %>%
  tq_transmute(select = adjusted,           # this specifies which column
to select
               mutate_fun = periodReturn,   # This specifies what to do
with that column
               period = "daily",      # This argument calculates Daily
returns
               col_rename = "idr_returns") # renames the column
Colonial_cum_returns <- Colonial_daily_returns %>%
  mutate(cr = cumprod(1 + idr_returns)) %>%      # using the cumprod
function
  mutate(cumulative_returns = cr - 1)

IAG_daily_returns <- IAG %>%
  tq_transmute(select = adjusted,           # this specifies which column
to select
               mutate_fun = periodReturn,   # This specifies what to do
with that column
               period = "daily",      # This argument calculates Daily
returns
               col_rename = "idr_returns") # renames the column
IAG_cum_returns <- IAG_daily_returns %>%
  mutate(cr = cumprod(1 + idr_returns)) %>%      # using the cumprod
function
  mutate(cumulative_returns = cr - 1)

Mapfre_daily_returns <- Mapfre %>%
  tq_transmute(select = adjusted,           # this specifies which column
to select
               mutate_fun = periodReturn,   # This specifies what to do
with that column
               period = "daily",      # This argument calculates Daily
returns
               col_rename = "idr_returns") # renames the column
Mapfre_cum_returns <- Mapfre_daily_returns %>%
  mutate(cr = cumprod(1 + idr_returns)) %>%      # using the cumprod
function
  mutate(cumulative_returns = cr - 1)

Melia_daily_returns <- Melia %>%
  tq_transmute(select = adjusted,           # this specifies which column
to select
               mutate_fun = periodReturn,   # This specifies what to do
with that column
               period = "daily",      # This argument calculates Daily
returns
               col_rename = "idr_returns") # renames the column
Melia_cum_returns <- Melia_daily_returns %>%
  mutate(cr = cumprod(1 + idr_returns)) %>%      # using the cumprod
function
  mutate(cumulative_returns = cr - 1)

Merlin_daily_returns <- Merlin %>%
  tq_transmute(select = adjusted,           # this specifies which column
to select
               mutate_fun = periodReturn,   # This specifies what to do
with that column
               period = "daily",      # This argument calculates Daily
returns
               col_rename = "idr_returns") # renames the column
Merlin_cum_returns <- Merlin_daily_returns %>%
  mutate(cr = cumprod(1 + idr_returns)) %>%      # using the cumprod
function
  mutate(cumulative_returns = cr - 1)

Naturgy_daily_returns <- Naturgy %>%
  tq_transmute(select = adjusted,           # this specifies which column
to select
               mutate_fun = periodReturn,   # This specifies what to do
with that column
               period = "daily",      # This argument calculates Daily
returns
               col_rename = "idr_returns") # renames the column
Naturgy_cum_returns <- Naturgy_daily_returns %>%
  mutate(cr = cumprod(1 + idr_returns)) %>%      # using the cumprod
function
  mutate(cumulative_returns = cr - 1)

REE_daily_returns <- REE %>%
  tq_transmute(select = adjusted,           # this specifies which column
to select
               mutate_fun = periodReturn,   # This specifies what to do
with that column
               period = "daily",      # This argument calculates Daily
returns
               col_rename = "idr_returns") # renames the column
REE_cum_returns <- REE_daily_returns %>%
  mutate(cr = cumprod(1 + idr_returns)) %>%      # using the cumprod
function
  mutate(cumulative_returns = cr - 1)

Repsol_daily_returns <- Repsol %>%
  tq_transmute(select = adjusted,           # this specifies which column
to select
               mutate_fun = periodReturn,   # This specifies what to do
with that column
               period = "daily",      # This argument calculates Daily
returns
               col_rename = "idr_returns") # renames the column
Repsol_cum_returns <- Repsol_daily_returns %>%
  mutate(cr = cumprod(1 + idr_returns)) %>%      # using the cumprod
function
  mutate(cumulative_returns = cr - 1)

SGamesa_daily_returns <- SGamesa %>%
  tq_transmute(select = adjusted,           # this specifies which column
to select
               mutate_fun = periodReturn,   # This specifies what to do
with that column
               period = "daily",      # This argument calculates Daily
returns
               col_rename = "idr_returns") # renames the column
SGamesa_cum_returns <- SGamesa_daily_returns %>%
  mutate(cr = cumprod(1 + idr_returns)) %>%      # using the cumprod
function
  mutate(cumulative_returns = cr - 1)

Telefonica_daily_returns <- Telefonica %>%
  tq_transmute(select = adjusted,           # this specifies which column
to select
               mutate_fun = periodReturn,   # This specifies what to do
with that column
               period = "daily",      # This argument calculates Daily
returns
               col_rename = "idr_returns") # renames the column
Telefonica_cum_returns <- Telefonica_daily_returns %>%
  mutate(cr = cumprod(1 + idr_returns)) %>%      # using the cumprod
function
  mutate(cumulative_returns = cr - 1)

Viscofan_daily_returns <- Viscofan %>%
  tq_transmute(select = adjusted,           # this specifies which column
to select
               mutate_fun = periodReturn,   # This specifies what to do
with that column
               period = "daily",      # This argument calculates Daily
returns
               col_rename = "idr_returns") # renames the column
Viscofan_cum_returns <- Viscofan_daily_returns %>%
  mutate(cr = cumprod(1 + idr_returns)) %>%      # using the cumprod
function
  mutate(cumulative_returns = cr - 1)

Acerinox_daily_returns <- Acerinox %>%
  tq_transmute(select = adjusted,           # this specifies which column
to select
               mutate_fun = periodReturn,   # This specifies what to do
with that column
               period = "daily",      # This argument calculates Daily
returns
               col_rename = "idr_returns") # renames the column
Acerinox_cum_returns <- Acerinox_daily_returns %>%
  mutate(cr = cumprod(1 + idr_returns)) %>%      # using the cumprod
function
  mutate(cumulative_returns = cr - 1)


Bankia_daily_returns <- Bankia %>%
  tq_transmute(select = adjusted,           # this specifies which column
to select
               mutate_fun = periodReturn,   # This specifies what to do
with that column
               period = "daily",      # This argument calculates Daily
returns
               col_rename = "idr_returns") # renames the column
Bankia_cum_returns <- Bankia_daily_returns %>%
  mutate(cr = cumprod(1 + idr_returns)) %>%      # using the cumprod
function
  mutate(cumulative_returns = cr - 1)

MasMovil_daily_returns <- MasMovil %>%
  tq_transmute(select = adjusted,           # this specifies which column
to select
               mutate_fun = periodReturn,   # This specifies what to do
with that column
               period = "daily",      # This argument calculates Daily
returns
               col_rename = "idr_returns") # renames the column
MasMovil_cum_returns <- MasMovil_daily_returns %>%
  mutate(cr = cumprod(1 + idr_returns)) %>%      # using the cumprod
function
  mutate(cumulative_returns = cr - 1)

Indra_daily_returns <- Indra %>%
  tq_transmute(select = adjusted,           # this specifies which column
to select
               mutate_fun = periodReturn,   # This specifies what to do
with that column
               period = "daily",      # This argument calculates Daily
returns
               col_rename = "idr_returns") # renames the column
Indra_cum_returns <- Indra_daily_returns %>%
  mutate(cr = cumprod(1 + idr_returns)) %>%      # using the cumprod
function
  mutate(cumulative_returns = cr - 1)

bestworst<-c(Acciona_cum_returns[nrow(Acciona_cum_returns),4],
             ACS_cum_returns[nrow(ACS_cum_returns),4],
             Aena_cum_returns[nrow(Aena_cum_returns),4],
             Amadeus_cum_returns[nrow(Amadeus_cum_returns),4],
             ArcelorMittal_cum_returns[nrow(ArcelorMittal_cum_returns),4],
             BBVA_cum_returns[nrow(BBVA_cum_returns),4],
             Sabadell_cum_returns[nrow(Sabadell_cum_returns),4],
             Santander_cum_returns[nrow(Santander_cum_returns),4],
             Bankinter_cum_returns[nrow(Bankinter_cum_returns),4],
             CaixaBank_cum_returns[nrow(CaixaBank_cum_returns),4],
             Cellnex_cum_returns[nrow(Cellnex_cum_returns),4],
             Enagas_cum_returns[nrow(Enagas_cum_returns),4],
             ENCE_cum_returns[nrow(ENCE_cum_returns),4],
             Endesa_cum_returns[nrow(Endesa_cum_returns),4],
             Ferrovial_cum_returns[nrow(Ferrovial_cum_returns),4],
             Grifols_cum_returns[nrow(Grifols_cum_returns),4],
             Iberdrola_cum_returns[nrow(Iberdrola_cum_returns),4],
             Inditex_cum_returns[nrow(Inditex_cum_returns),4],
             Colonial_cum_returns[nrow(Colonial_cum_returns),4],
             IAG_cum_returns[nrow(IAG_cum_returns),4],
             Mapfre_cum_returns[nrow(Mapfre_cum_returns),4],
             Melia_cum_returns[nrow(Melia_cum_returns),4],
             Merlin_cum_returns[nrow(Merlin_cum_returns),4],
             Naturgy_cum_returns[nrow(Naturgy_cum_returns),4],
             REE_cum_returns[nrow(REE_cum_returns),4],
             Repsol_cum_returns[nrow(Repsol_cum_returns),4],
             SGamesa_cum_returns[nrow(SGamesa_cum_returns),4],
             Telefonica_cum_returns[nrow(Telefonica_cum_returns),4],
             Viscofan_cum_returns[nrow(Viscofan_cum_returns),4],
             Acerinox_cum_returns[nrow(Acerinox_cum_returns),4],
             Bankia_cum_returns[nrow(Bankia_cum_returns),4],
             CIE_cum_returns[nrow(CIE_cum_returns),4],
             MasMovil_cum_returns[nrow(MasMovil_cum_returns),4],
             Almirall_cum_returns[nrow(Almirall_cum_returns),4],
             Indra_cum_returns[nrow(Indra_cum_returns),4])

namebw<-c("Acciona",
          "ACS",
          "Aena",
              "Amadeus",
          "ArcelorMittal",
          "BBVA",
          "Sabadell",
          "Santander",
          "Bankinter",
          "CaixaBank",
          "Cellnex",
               "Enagas",
          "ENCE",
               "Endesa",
          "Ferrovial",
               "Grifols",
          "Iberdrola",
               "Inditex",
          "Colonial",
               "IAG",
          "Mapfre",
               "Melia",
          "Merlin",
               "Naturgy",
          "REE",
               "Repsol",
          "SGamesa",
               "Telefonica",
          "Viscofan",
               "Acerinox",
          "Bankia",
               "CIE",
          "MasMovil",
               "Almirall",
          "Indra")


bwfinal <- matrix(bestworst, nrow =35 , ncol = 1)
bwfinal2 <- matrix(namebw, nrow =35 , ncol = 1)

bwc<-cbind(bwfinal2,bwfinal)
colnames(bwc)=c("Accion","reval")
bwc <- as.data.frame(bwc)
colnames(bwchist)=c("Accion","reval")
bwchist <-as.data.frame(bwc[order(bwc$reval), ])
#
Dear Pedro

Some comments in-line
On 30/07/2020 21:16, Pedro p?ramo wrote:
So bwchist is not a numeric variable as hist needs. Aboce you said it is 
a data frame but data frames are not numeric.

For future reference your example is way too long for anyone to go 
through and try to help you. Try next time to reduce it to the absolute 
minimum by removing sections while you still get the error. It is also 
easier to get help if you can remove unnecessary packages.

It is also unreadable because you are posting in HTML and that makes the 
post unreadable as this is a plain text list.

Michael

  
    
  
#
Hello,

I second Michael's opinion. When the post's code is very long, there is 
a tendency to have less answers.

Please post the output of


dput(head(bwchist, 30))


It's much shorter code and it recreates the data so we will be able to 
see what's wrong and try to find a solution.

Hope this helps,

Rui Barradas


?s 15:44 de 31/07/2020, Michael Dewey escreveu:

  
    
#
Hi Pedro,

Another 'best practice' and polite thing to do is link to other places
you may have cross-posted.  That will give people the opportunity to
see if your questions has been answered in another forum.

I saw your post on R-SIG-Finance
(https://stat.ethz.ch/pipermail/r-sig-finance/2020q3/014979.html), and
started to work on a solution.

I don't know how to do this in tidyquant, but here's how you can do it
with quantmod:

# all tickers
tk <- c("ANA.MC", "ACS.MC", "AENA.MC", "AMS.MC", "MTS.MC", "BBVA.MC", "SAB.MC",
  "SAN.MC", "BKT.MC", "CABK.MC", "CLNX.MC", "ENG.MC", "ENC.MC", "ELE.MC",
  "FER.MC", "GRF.MC", "IBE.MC", "ITX.MC", "COL.MC", "IAG.MC", "MAP.MC",
  "MEL.MC", "MRL.MC", "NTGY.MC", "REE.MC", "REP.MC", "SGRE.MC", "TEF.MC",
  "VIS.MC", "ACX.MC", "BKIA.MC", "CIE.MC", "MAS.MC", "ALM.MC", "IDR.MC")

# download them into an environment ('e')
require(quantmod)
getSymbols(tk, from = "2019-12-31", env = (e <- new.env()))

# extract adjusted close column
adj <- lapply(e, Ad)
# calculate daily returns from adjusted data,
# merge into a xts matrix, and fill NA with 0
ret <- do.call(merge, c(lapply(adj, dailyReturn), fill = 0))
# cumulative returns
cumret <- cumprod(1 + ret) - 1
# set names
colnames(cumret) <- names(adj)
last(cumret)
# calculate histogram for period-to-date returns
hist(drop(last(cumret)))

I'm not sure that's the histogram you're looking for, but I hope it
gives you a start toward a solution.

Best,
Josh
On Fri, Jul 31, 2020 at 9:55 AM Rui Barradas <ruipbarradas at sapo.pt> wrote:

  
    
#
On 2020-07-31 10:07 -0500, Joshua Ulrich wrote:
| On Fri, Jul 31, 2020 at 9:55 AM Rui Barradas wrote:
| | ?s 15:44 de 31/07/2020, Michael Dewey escreveu:
| | | Dear Pedro
| | |
| | | Some comments in-line
| | |
| | | On 30/07/2020 21:16, Pedro p?ramo wrote:
| | | | Hi all,
| | | |
| | | | I attach my code, the think is I 
| | | | want to make a bar plot the last 
| | | | variable called "bwchist" so the 
| | | | X axis are "Accion" and the y 
| | | | axis are "reval" values.
| | | |
| | | | I have prove class(bwchist) and 
| | | | says dataframe but its still a 
| | | | list because it says me I have 
| | | | prove to unlist, but it doesnt 
| | | | work
| | | |
| | | | hist(bwchist)
| | | | Error in hist.default(bwchist) : 'x' must be numeric
| | |
| | | So bwchist is not a numeric 
| | | variable as hist needs. Aboce you 
| | | said it is a data frame but data 
| | | frames are not numeric.
| | |
| | | For future reference your example 
| | | is way too long for anyone to go 
| | | through and try to help you. Try 
| | | next time to reduce it to the 
| | | absolute minimum by removing 
| | | sections while you still get the 
| | | error.  It is also easier to get 
| | | help if you can remove unnecessary 
| | | packages.
| | |
| | | It is also unreadable because you 
| | | are posting in HTML and that makes 
| | | the post unreadable as this is a 
| | | plain text list.
| |
| | Hello,
| |
| | I second Michael's opinion. When the 
| | post's code is very long, there is a 
| | tendency to have less answers.
| |
| | Please post the output of
| |
| | 	dput(head(bwchist, 30))
| |
| | It's much shorter code and it 
| | recreates the data so we will be 
| | able to see what's wrong and try to 
| | find a solution.
| 
| Hi Pedro,
| 
| Another 'best practice' and polite 
| thing to do is link to other places 
| you may have cross-posted.  That will 
| give people the opportunity to see if 
| your questions has been answered in 
| another forum.
| 
| I saw your post on R-SIG-Finance 
| (https://stat.ethz.ch/pipermail/r-sig-finance/2020q3/014979.html), 
| and started to work on a solution.
| 
| I don't know how to do this in 
| tidyquant, but here's how you can do 
| it with quantmod:
| 
| # all tickers
| tk <- c("ANA.MC", "ACS.MC", "AENA.MC", "AMS.MC", "MTS.MC", "BBVA.MC", "SAB.MC",
|   "SAN.MC", "BKT.MC", "CABK.MC", "CLNX.MC", "ENG.MC", "ENC.MC", "ELE.MC",
|   "FER.MC", "GRF.MC", "IBE.MC", "ITX.MC", "COL.MC", "IAG.MC", "MAP.MC",
|   "MEL.MC", "MRL.MC", "NTGY.MC", "REE.MC", "REP.MC", "SGRE.MC", "TEF.MC",
|   "VIS.MC", "ACX.MC", "BKIA.MC", "CIE.MC", "MAS.MC", "ALM.MC", "IDR.MC")
| 
| # download them into an environment ('e')
| require(quantmod)
| getSymbols(tk, from = "2019-12-31", env = (e <- new.env()))
| 
| # extract adjusted close column
| adj <- lapply(e, Ad)
| # calculate daily returns from adjusted data,
| # merge into a xts matrix, and fill NA with 0
| ret <- do.call(merge, c(lapply(adj, dailyReturn), fill = 0))
| # cumulative returns
| cumret <- cumprod(1 + ret) - 1
| # set names
| colnames(cumret) <- names(adj)
| last(cumret)
| # calculate histogram for period-to-date returns
| hist(drop(last(cumret)))
| 
| I'm not sure that's the histogram 
| you're looking for, but I hope it 
| gives you a start toward a solution.
| 
| Best,
| Josh

Wow Josh!  That's very elegant.  

Myself now, I just plowed through the 
original code to make it simpler, but am 
at a loss as to how this histogram looks 
...

	x <- c("ANA.MC", "ACS.MC", "AENA.MC", "AMS.MC", "MTS.MC", "BBVA.MC",
	  "SAB.MC", "SAN.MC", "BKT.MC", "CABK.MC", "CLNX.MC", "ENG.MC",
	  "ENC.MC", "ELE.MC", "FER.MC", "GRF.MC", "IBE.MC", "ITX.MC",
	  "COL.MC", "IAG.MC", "MAP.MC", "MEL.MC", "MRL.MC", "NTGY.MC",
	  "REE.MC", "REP.MC", "SGRE.MC", "TEF.MC", "VIS.MC", "ACX.MC",
	  "BKIA.MC", "CIE.MC", "MAS.MC", "ALM.MC", "IDR.MC")
	stock.prices <- 
	  lapply(x, function(stock) {
	    tidyquant::tq_get(x=stock,from = '2019-12-31',get = "stock.prices")
	  })
	names(stock.prices) <- x
	
	library(tidyquant)
	
	returns <- lapply(stock.prices, function(data) {
	  tab <-
	    tq_transmute(
	      data = data,
	      select = adjusted,           # this specifies which column to select
	      mutate_fun = periodReturn,   # This specifies what to do with that column
	      period = "daily",            # This argument calculates Daily returns
	      col_rename = "idr_returns")  # renames the column
	  tab[,"cr"] <- cumprod(1 + tab[,"idr_returns"])
	  tab[,"cumulative_returns"] <- tab[,"cr"] - 1
	
	  dplyr::pull(
	    tab[nrow(tab[,"cumulative_returns"]),
	                  "cumulative_returns"]
	  )
	})
	
	bestworst <- simplify2array(returns)
	
	namebw <- 
	  c("Acciona", "ACS", "Aena", "Amadeus",
	    "ArcelorMittal", "BBVA", "Sabadell",
	    "Santander", "Bankinter",
	    "CaixaBank", "Cellnex", "Enagas",
	    "ENCE", "Endesa", "Ferrovial",
	    "Grifols", "Iberdrola", "Inditex",
	    "Colonial", "IAG", "Mapfre",
	    "Melia", "Merlin", "Naturgy", "REE",
	    "Repsol", "SGamesa", "Telefonica",
	    "Viscofan", "Acerinox", "Bankia",
	    "CIE", "MasMovil", "Almirall",
	    "Indra")
	
	bwc <- data.frame(
	  symbol=names(bestworst),
	  Accion=namebw,
	  reval=bestworst)

| | | | bwc<-cbind(bwfinal2,bwfinal)
| | | | colnames(bwc)=c("Accion","reval")
| | | | bwc <- as.data.frame(bwc)

... aaaand you know something's 
happening between here (where bwchist is 
created), but you don't know what it is, 
do you, Mr p?ramo?

| | | | colnames(bwchist)=c("Accion","reval")
| | | | bwchist <-as.data.frame(bwc[order(bwc$reval), ])

Best,
Rasmus

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20200731/a8fcb94d/attachment.sig>