2

I am trying to use read.csv() command, but I do not understand colClasses part to run coding. Does anyone explain what it is, and also give me example of simple coding for read.csv()?

Also, if I run my coding for read.csv(), I get an error

> object of type 'closure' is not subsettable

What type of error is this? Last time I run my code, it worked, but now I get this. I am not sure what change I should make here. This is my code:

Precipfiles[1:24] <- list.files(pattern=".csv")
> DF <- NULL
> for (f in Precipfiles[1:24]) {
    data[1:24]<-read.csv(f,header=T,sep="\t",na.string="",colClasses="character")
    DF[1:24]<-rbind(DF,data[1:24])
}

Basically, I load all data and put them together, but I have not able to use merge() command since I am having troubles I listed above.

I think I should not use colClasses="character" because data I am using are all numeric in 200 by 200 matrix. There are 24 data files that I have to put them together.

If you have any suggestions and advise to improve this coding please let me know. Thank you for all of your help.

bartektartanus
  • 15,284
  • 6
  • 74
  • 102
  • 1
    Try remove all `[1:24]` indices – Rcoster Feb 24 '13 at 22:09
  • 2
    Possible dublicate of http://stackoverflow.com/questions/11308367/object-of-type-closure-is-not-subsettable – EDi Feb 24 '13 at 22:11
  • 1
    It would be interesting to collect on SO questions that are related to using common function names as variables. `data`, `df` etc. – Ricardo Saporta Feb 24 '13 at 22:38
  • Please do not edit your question such that it changes the complete meaning. If you have a new (followup) question, search for an answer, then if you can't find one, post as a separate question. – mnel Feb 26 '13 at 22:27

1 Answers1

5

You really don't need the [1:24] in every assignment, this is what is causing your problems. You are assign to a subset of a indexed vector of some description.

The error message when are trying to assign to data[1:24], without data being assigned previously (in your previous usage (which you mentioned worked), data was probably a list or data.frame you had created.). As such data is a function (for loading data associated with packages, see ?data) and the error you saw is saying that (a function includes a closure)

I would suggest something like

Precipfiles <- list.files(pattern=".csv")
DFlist <- lapply(Precipfiles, read.table, sep = '\t', 
                  na.string = '', header = TRUE)
bigDF <- do.call(rbind, DFlist)

# or, much faster using data.table 
library(data.table)
bigDF <- rbindlist(DFlist)
agstudy
  • 119,832
  • 17
  • 199
  • 261
mnel
  • 113,303
  • 27
  • 265
  • 254
  • Never thought of using `rbindlist` on data.frames. +1! – Ricardo Saporta Feb 25 '13 at 01:48
  • mnel, I tried your code, and it much faster, somewhat. Thank you for your help and advice. I thought without [1:24], files becomes one big file and I cannot call one particular number from one particular data file. –  Feb 25 '13 at 20:29
  • Ricardo Saporta, Why shouldn't I use rbindlist on data.frames? –  Feb 25 '13 at 20:30