1

I cannot figure out what's going wrong with my loop and it is already too complicated for my current level. I have already tried applybut obviously I do something wrong, so I didn't use it at all.

library('wavelets')
library('benford.analysis')

indeces <- ls() # my initial datasets
wfilters <- array(c("haar","la8","d4","c6")) # filter option in "modwt" function
wfiltname <- array(c("h","l","d","c")) # to rename the new objects

for (i in 1:nrow(as.array(indeces))) { 
  x <- get(as.matrix(indeces[i]))
  x <- x[,2]
  # Creates modwt objects equal to the number of filters
  for (j in 1:nrow(as.array(wfilters))) {
    x <- wavelets::modwt(x, filter = wfilters[j], n.levels = 4,
                         boundary = "periodic")
    # A loop that creates a matrix with benford fun output per modwt n.levels option
    for (l in 1:4) {
      x <- as.matrix(x@W$W[l]) # n.levels are represented as x@W$W1, x@W$W2,...
      x <- benford.analysis::benford(x, number.of.digits = 1,
                                     sign = "both", discrete = T,
                                     round = 3) # accepts matrices
      x[,l] <- x$bfd$data.dist # it always has 9 elements
    }
    assign(paste0("b", wfiltname[j], indeces[i]), x)
  }
} 

The above loop should be reproducible with any data (where the values are in second column). The error I get is the following:

Error in array(x, c(length(x), 1L), if (!is.null(names(x))) list(names(x),  : 
  'data' must be of a vector type, was 'NULL'
3
  • I haven't been to all your code but you can start by changing get(as.matrix(indeces[i])) into as.matrix(get(indeces[i])). Commented Feb 20, 2017 at 14:59
  • Recommendation for debugging: put browse() in a place before the occurance of the error. BTW: eventually for (i in 1:length(indeces)) is equivalent to for (i in 1:nrow(as.array(indeces))) Commented Feb 20, 2017 at 14:59
  • Thank you for your comments. I'll fix them and I'll edit my post. Commented Feb 20, 2017 at 15:36

1 Answer 1

1

Thanks to @Cath and @jogo I made it work after some improvements. Here's the correct code:

temp <- list.files(path = "...")
list2env(
  lapply(setNames(temp, make.names(gsub("*.csv$", "", temp))), 
         read.csv), envir = .GlobalEnv)
rm(temp)

indeces <- ls()
wfilters <- array(c("haar","la8","d4","c6"))
wfiltname <- array(c("h","l","d","c"))
k <- data.frame(matrix(nrow = 9,ncol = 4))
nlvl <- 4

for (i in 1:length(indeces)) {
  x <- as.matrix(get(indeces[i]))
  for (j in 1:length(wfilters)) {
    y <- wavelets::modwt(as.matrix(x), filter = wfilters[j], n.levels = nlvl,
                         boundary = "periodic")
    y <- as.matrix(y@W)
    for(m in 1:nlvl) {
      z <- as.matrix(y[[m]])
      z <- benford.analysis::benford(z, number.of.digits = 1, sign = "both", discrete = TRUE, round = 16)
      k[m] <- as.data.frame(z$bfd$data.dist)
      colnames(k)[m] <- paste0(wfilters[j], "W", m)
    }
    assign(paste0(indeces[i], wfiltname[j]), k)
  }
}
rm(x,y,z,i,j,m,k)

I would appreciate if there is a way to write it more efficiently. Thank you very much

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.