How to merge multiple dataframes with different names

Hi
it's been a while i'm searching for help but i didn't found it
I would like to merge multiple dataframes (xts in facts) all with different name
so i can't use a loop and i can't use reduce with merge (i'll have 505 names to type).
I would like to get All the S&P500 stocks data and merge them together.
This is my code
Please install tidyquant or quantmode first

#obtenir le contenu de mon index

symb=tq_index("SP500")

#Obtenir mes données (trycatch sert a continuer malgré mon erreur cette fn marche mais peut etre optiisée)

for(i in(1:505)){tryCatch({(getSymbols(symb$symbol[i], from = '2017-01-01',
           to = "2018-03-01",warnings = FALSE,
           auto.assign = TRUE))
  }, error=function(e){cat("ERROR :",conditionMessage(e), "\n")})
}

#Mettre ensemble mes tableaux

stockname<-list(noquote(symb$symbol))
fusion=Reduce(function(x,y)merge(x,y),stockname)

when i run my reduce like that i get a charachter vector with my stockname but i want an object with stocknames and data

There's an issue with installing tidyquant, so I can't run tq_index() -- is this common, as far as you're aware?

Dont't install it with install.packages
install it by clicking on tools and install packages then you type tydiquant

That's the way I installed it, but got this error when I tried to load it:

Error: package or namespace load failed for ‘tidyquant’:
 .onAttach failed in attachNamespace() for 'tidyquant', details:
  call: NULL
  error: Function getThemeInfo not found in RStudio

Does this work for you?

library(tidyquant)
library(tidyverse)

symb = tq_index("SP500")

for (i in(1:505)) {
  tryCatch({
    (
      getSymbols(
        symb$symbol[i],
        from = '2017-01-01',
        to = "2018-03-01",
        warnings = FALSE,
        auto.assign = TRUE
      )
    )
  }, error = function(e) {
    cat("ERROR :", conditionMessage(e), "\n")
  })
}

stocknames <- ls(pattern = "[A-Z]{1,4}") %>%
  set_names()

fusion <- map_dfc(stocknames, ~ as_tibble(eval(sym(.))))

fusion
#> # A tibble: 291 x 2,988
#>    A.Open A.High A.Low A.Close A.Volume A.Adjusted AAL.Open AAL.High AAL.Low
#>     <dbl>  <dbl> <dbl>   <dbl>    <dbl>      <dbl>    <dbl>    <dbl>   <dbl>
#>  1   45.9   46.8  45.7    46.5  1739600       45.3     47.3     47.3    46.1
#>  2   46.9   47.4  46.8    47.1  1821300       45.9     46.6     47.4    46.3
#>  3   47.0   47.1  46.4    46.5  1503700       45.3     46.5     46.9    45.6
#>  4   46.6   48.1  46.6    48.0  2883400       46.7     45.8     46.7    45.5
#>  5   48.0   48.6  47.9    48.1  2575300       46.9     46.0     47.3    45.8
#>  6   48.3   48.5  47.8    48.1  1560500       46.8     47       48.5    47.0
#>  7   48.0   49.5  48.0    49.2  5624300       48.0     49.3     49.9    48.1
#>  8   48.9   48.9  48.0    48.5  3276900       47.2     48.7     49      46.8
#>  9   48.6   48.8  48.4    48.7  1568500       47.4     48.2     48.4    47.5
#> 10   48.4   48.7  48.2    48.3  1938800       47.0     47.6     47.8    46.7
#> # … with 281 more rows, and 2,979 more variables: AAL.Close <dbl>,
#> #   AAL.Volume <dbl>, AAL.Adjusted <dbl>, AAP.Open <dbl>, AAP.High <dbl>,
#> #   AAP.Low <dbl>, AAP.Close <dbl>, AAP.Volume <dbl>, AAP.Adjusted <dbl>,
#> #   AAPL.Open <dbl>, AAPL.High <dbl>, AAPL.Low <dbl>, AAPL.Close <dbl>,
#> #   AAPL.Volume <dbl>, AAPL.Adjusted <dbl>, ABBV.Open <dbl>, ABBV.High <dbl>,
#> #   ABBV.Low <dbl>, ABBV.Close <dbl>, ABBV.Volume <dbl>, ABBV.Adjusted <dbl>,
#> #   ABC.Open <dbl>, ABC.High <dbl>, ABC.Low <dbl>, ABC.Close <dbl>,
#> #   ABC.Volume <dbl>, ABC.Adjusted <dbl>, ABMD.Open <dbl>, ABMD.High <dbl>,
#> #   ABMD.Low <dbl>, ABMD.Close <dbl>, ABMD.Volume <dbl>, ABMD.Adjusted <dbl>,
#> #   ABT.Open <dbl>, ABT.High <dbl>, ABT.Low <dbl>, ABT.Close <dbl>,
#> #   ABT.Volume <dbl>, ABT.Adjusted <dbl>, ACN.Open <dbl>, ACN.High <dbl>,
#> #   ACN.Low <dbl>, ACN.Close <dbl>, ACN.Volume <dbl>, ACN.Adjusted <dbl>,
#> #   ADBE.Open <dbl>, ADBE.High <dbl>, ADBE.Low <dbl>, ADBE.Close <dbl>,
#> #   ADBE.Volume <dbl>, ADBE.Adjusted <dbl>, ADI.Open <dbl>, ADI.High <dbl>,
#> #   ADI.Low <dbl>, ADI.Close <dbl>, ADI.Volume <dbl>, ADI.Adjusted <dbl>,
#> #   ADM.Open <dbl>, ADM.High <dbl>, ADM.Low <dbl>, ADM.Close <dbl>,
#> #   ADM.Volume <dbl>, ADM.Adjusted <dbl>, ADP.Open <dbl>, ADP.High <dbl>,
#> #   ADP.Low <dbl>, ADP.Close <dbl>, ADP.Volume <dbl>, ADP.Adjusted <dbl>,
#> #   ADS.Open <dbl>, ADS.High <dbl>, ADS.Low <dbl>, ADS.Close <dbl>,
#> #   ADS.Volume <dbl>, ADS.Adjusted <dbl>, ADSK.Open <dbl>, ADSK.High <dbl>,
#> #   ADSK.Low <dbl>, ADSK.Close <dbl>, ADSK.Volume <dbl>, ADSK.Adjusted <dbl>,
#> #   AEE.Open <dbl>, AEE.High <dbl>, AEE.Low <dbl>, AEE.Close <dbl>,
#> #   AEE.Volume <dbl>, AEE.Adjusted <dbl>, AEP.Open <dbl>, AEP.High <dbl>,
#> #   AEP.Low <dbl>, AEP.Close <dbl>, AEP.Volume <dbl>, AEP.Adjusted <dbl>,
#> #   AES.Open <dbl>, AES.High <dbl>, AES.Low <dbl>, AES.Close <dbl>,
#> #   AES.Volume <dbl>, AES.Adjusted <dbl>, AFL.Open <dbl>, …

Created on 2020-02-22 by the reprex package (v0.3.0)

dromano: unh.... i really don't know why it's not working

andresrcs: oh god thanks for that it's working thanks a lot.

If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.