Pregunta

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.

¿Fue útil?

Solución

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)
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top