[R] hist from a list
David Winsemius
dw|n@em|u@ @end|ng |rom comc@@t@net
Tue Mar 9 00:15:40 CET 2021
On 8/3/20 11:48 AM, Pedro páramo wrote:
> Hi Rasmus, Josh and Rui,
>
> First of all many thanks in advance about your help.
>
> The first thig is sometimes you say " you are posting in HTML and that
> makes the
> post unreadable as this is a plain text list" how can I put the code in the
> correct way, not html (attaching in txt?)
Set your gmail client to deliver in plain text.
Attached is a png file that is a screen shot of a Chrome mediated effort
at composing a message and clicking the three-stacked-dots icon at the
lower right corner. (Learn to use your chosen software tools by reading
manuals or searching.)
--
David.
>
> The second about the code:
>
> I have used this:
>
> bwc <- cbind(bwfinal2,bwfinal)
> colnames(bwc)=c("Accion","reval")
> df <- matrix(unlist(bwc), nrow=nrow(bwc), byrow=F)
> colnames(bwchist)=c("Accion","reval")
> bwchist <-as.data.frame(bwc[order(df[,2]), ])
>
> bwchist is the ordered cum stock returns in the year but because is a list
> it is not possible to plot and histogram with x (names of stocks) and the x
> axist the value of cum stocks (reval)
>
> when I put dput(bwchist) the console says:
>
> dput(bwchist)
> structure(list(Accion = list("REE", "Enagas", "Grifols", "Ferrovial",
> "Acerinox", "Naturgy", "Inditex", "Bankia", "ENCE", "Aena",
> "Bankinter", "Mapfre", "CaixaBank", "CIE", "Colonial", "Almirall",
> "Indra", "ArcelorMittal", "ACS", "Telefonica", "Amadeus",
> "BBVA", "Merlin", "Santander", "Repsol", "Melia", "Sabadell",
> "IAG", "Acciona", "Endesa", "MasMovil", "Iberdrola", "SGamesa",
> "Viscofan", "Cellnex"), reval = list(-0.0200827282700085,
> -0.0590294115600855, -0.214126598790964, -0.220773677809979,
> -0.229653300324357, -0.257944379583984, -0.283942789063822,
> -0.285159347392533, -0.303814713896458, -0.30734460425763,
> -0.309408155539818, -0.319912221435868, -0.322790949659181,
> -0.344047579452905, -0.347919538415482, -0.356898907103825,
> -0.374263261296661, -0.40147247119078, -0.405150043834815,
> -0.406022775042175, -0.413786100987797, -0.440679109311707,
> -0.442603156492871, -0.491634140733524, -0.499254932434042,
> -0.6, -0.709737357505148, -0.724461258850966, 0.0220528711420083,
> 0.0462767672643172, 0.115044247787611, 0.238734548714937,
> 0.274578114644054, 0.343422896082666, 0.387826126094928)), class =
> "data.frame", row.names = c(NA,
> -35L))
>
> I try to make an hist or barplot but because it is a list no way to obtain
> the plot.
>
> Many thanks again for your help.
>
> I have printed two manuals to improve my level, but if you can help me, I
> would be very very gratefull.
>
>
>
> El vie., 31 jul. 2020 a las 18:28, Rasmus Liland (<jral using posteo.no>)
> escribió:
>
>> 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
>>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Screenshot from 2021-03-08 15-12-07.png
Type: image/png
Size: 32747 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20210308/d52ce55d/attachment.png>
More information about the R-help
mailing list