-1

I am trying to covert many time series xts objects to tibbles, and the for loop I wrote does not work properly, I don't know why.

This does not only happen to this particular task, but other task I perform, I have a list called "code", which contains a list of names for all the xts objects I want to convert from.

code <- c('ABT','BA','CL','ROK')

for (i in code)
  {
  i <- tk_tbl(i, preserve_index = TRUE, rename_index = "index",
       timetk_idx = FALSE, silent = FALSE)
}

What is strange is that, if I use a single one without loop, it works beautifully and convert the xts "ABT" to a tibble "ABT"

ABT <- tk_tbl(ABT, preserve_index = TRUE, rename_index = "index",
       timetk_idx = FALSE, silent = FALSE)

The error message for the first code is

Warning: No index to preserve. Object otherwise converted to tibble successfully.

38: In tk_tbl.data.frame(as.data.frame(data), preserve_index, ... :

Edit:

tk_tabl is a function from the package timetk, and it "Coerce time-series objects to tibble."
And code is a vector containing names.

library(timetk)
code <- c('ABT','BA','CL','ROK')


> dput(head(ROK))
structure(c(8.14062, 8.15625, 8.03125, 7.78125, 7.6875, 7.71875, 
8.25, 8.15625, 8.125, 7.90625, 7.71875, 7.75, 8.03125, 8.125, 
7.90625, 7.65625, 7.625, 7.65625, 8.1875, 8.125, 7.90625, 7.71875, 
7.65625, 7.6875, 109600, 80800, 138400, 151600, 96800, 258800, 
0.684505, 0.67928, 0.660992, 0.645316, 0.640091, 0.642704), 
class=c("xts", "zoo"), .indexCLASS = "Date", tclass = "Date", 
.indexTZ = "UTC", tzone = "UTC", src = "yahoo", 
updated = structure(1558826745.23035, class = c("POSIXct","POSIXt")), 
index = structure(c(378604800, 378950400, 379036800, 
379123200, 379209600, 379296000), tzone = "UTC", tclass = "Date"), 
.Dim = c(6L, 6L), .Dimnames = list(NULL, c("ROK.Open", "ROK.High",
"ROK.Low", "ROK.Close", "ROK.Volume", "ROK.Adjusted")))
Cettt
  • 11,460
  • 7
  • 35
  • 58
The R
  • 135
  • 7
  • 1
    Please consider making this question reproducible. We have no idea what `A`, `company`, or `tk_tbl` are (other than guessing that `company` is a frame or list, and `tk_tbl` is a function you wrote). It would be useful to give use unambiguous sample data using `dput(head(x))` (for any/all data relevant), and the source (or package ... `timetk`?) of the function. https://stackoverflow.com/questions/5963269, https://stackoverflow.com/help/mcve, and https://stackoverflow.com/tags/r/info. – r2evans May 25 '19 at 23:56
  • @r2evans Thank you very much for your reminder, I have edited the question and hope this can make things clearer – The R May 26 '19 at 00:04
  • 2
    Aren't you like saving the result of your loop in a way it overwrites `i`? Is this intended? Wouldn't you want `for (i in company$code) {new.data[i] <- tk_tbl(...)}` – z-cool May 26 '19 at 00:07
  • @z-cool It's my stupid way to convert xts "i"s to tibble "i"s by overwriting them, maybe it's wrong? I tried your code but it gives "Error: object 'new.data' not found". – The R May 26 '19 at 00:11
  • 1
    In the first pass through the `for` loop, it creates a variable named `i` and fills it with the first value on `company$code`. You then overwrite `i` with the results of `tk_tbl(i,...)`, so allegedly `i` is now a `tibble`. And on the next pass through the loop, the value if `i` is over-written with the next value from `company$code`, thereby destroying the results from the recent run of `tk_tbl`. When the `for` loop is complete, your `i` contains the results from the *last* run of `tk_tbl(i,...)`. – r2evans May 26 '19 at 00:15
  • 1
    Perhaps: `all_i_values <- lapply(company$code, function(i) tk_tbl(i, ...))`? – r2evans May 26 '19 at 00:15
  • @r2evans It seems correct, but gives same error and all_i_values output is a list of 50. (50 is the amount of names in "code"). Btw, I am really sorry for my sloppy question. – The R May 26 '19 at 00:33
  • Sorry, I have speculated myself out here ... I don't know anything specific about `timetk` or `tk_tbl`, so I'm done. My next step (if I had to use it) would be to `debug(tk_tbl)` and find out what is causing that problem. BTW: that is a `Warning:`, not an `Error:`; this seems semantic, but is important. Granted, some packages use `warning(...)` when they should use `stop(...)`. – r2evans May 27 '19 at 01:02

1 Answers1

0

For me it looks like that you expect <- to do what assign is doing.

I think you get your expected result when you change your loop to:

for (i in code) {
  assign(i, tk_tbl(i, preserve_index = TRUE, rename_index = "index", timetk_idx = FALSE, silent = FALSE))
}
GKi
  • 37,245
  • 2
  • 26
  • 48