Skip to content

Reshape: 'melt' numerous objects

2 messages · Steve Murray, Hadley Wickham

#
Dear R Users,

I'm trying to use the reshape package to 'melt' my gridded data into column format. I've done this before on individual files, but this time I'm trying to do it on a directory of files (with variable file names) - therefore I have to also use the 'assign' command. I have come up against a couple of problems however and am therefore seeking advice...
Error in paste("Fekete_table_temp", index$year[i], index$month[i], sep = "")$Latitude : 
  $ operator is invalid for atomic vectors


To get round this, I did:

assign(paste("Fekete_table_temp", index$year[i], index$month[i], sep='')["Latitude"],rownames(Fekete_198601))


And for the actual loop in which the files are melted, I tried:

for (i in 1:120) {
Fekete_table_temp <- get(paste("Fekete_", index$year[i], index$month[i], sep=''))
Fekete_table_long <- melt(Fekete_table_temp, id.var="Latitude", na.rm=TRUE)
assign(paste("Fekete_long_", index$year[i], index$month[i], sep=''), Fekete_table_long)
names(paste("Fekete_long_", index$year[i], index$month[i], sep=''), c("Latitude", "Longitude", paste("Obs",index$year[i], index$month[i], sep=''))
        }


However, this results in:

Error: id variables not found in data: Latitude

...despite me having (supposedly) told it where 'Latitude' is, in 'Fekete_table_temp'.


What have I done wrong here?! And more importantly, how do I put it right?!


Many thanks for any help,

Steve


_________________________________________________________________
[[elided Hotmail spam]]
#
On Tue, Mar 31, 2009 at 11:12 AM, Steve Murray <smurray444 at hotmail.com> wrote:
I'd _strongly_ recommend you don't use assign.  Instead put everything
in a list:

paths <- dir("mydir", "\\.csv$", full.names = TRUE)
names(paths) <- basename(paths)

data <- lapply(paths, read.csv)
molten <- lapply(data, melt, id = "Latitude", na.rm=TRUE)

Hadley