0

I am trying to save a histogram for every file in a list. I cannot load more than 1 file at a time due to their large size. Normally I would use a symbolic object name for each file's histogram and iterate the name for each item in the list. I am having trouble figuring out how to do this in R so instead I attempt to save each hist as a column of a data.frame. The code is as follows:

filelist <- list.files("dir/")
file.hist <- data.frame(check.rows = FALSE)
for(i in 1:length(filelist) {
  file <- read.csv(capture.output(cat("dir/", filelist[i], sep = "")))
  file.hist[[i]] <- hist(file$Value, breaks = 200)
}

The error message that results is:

Error in `[[<-.data.frame`(`*tmp*`, i, value = list(breaks = c(0, 200, : 
  replacement has 6 rows, data has 0

I have googled the error message and it seems like it might be related to how you go about initializing the data from although I have to admit that my brain is fried this close to Thanksgiving. Has anyone out there dealt with an solved a similar problem? I am not married to this approach.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Sledge
  • 1,245
  • 1
  • 23
  • 47
  • Are you trying to save the image of a histogram? you should use an imaging device like the one referred to [here](http://stackoverflow.com/questions/7144118/how-to-save-a-plot-as-image-on-the-disk) – Tony Laidig Nov 27 '13 at 21:46
  • 4
    If you _are_ trying to save the `hist` object, you should be using a list rather than a data frame (and you should pre-allocate the list to be the desired length up front). – joran Nov 27 '13 at 21:47
  • Thanks for the helpful comments, I am trying to save the hist object. I will try the pre-allocated list method. – Sledge Nov 27 '13 at 21:49
  • 1
    Try replacing `file.hist <- data.frame(check.rows = FALSE)` with `file.hist <- vector("list", length(filelist))` in your code. – ialm Nov 27 '13 at 22:23

0 Answers0