0

This is part of my code.

library(reshape2)
setwd("C:/Users/Desktop/WildFires/FedFire8004/FedFire8004")
load("fedfire8004.rda")
library(reshape2)
Acres <- melt(fedfire8004$acres)

It reads data which has lat,lon,time(monthly) and value and converts data to below format(Acres). The problem is that in output there is no difference between month 1 and month 10. They both are stored under for example 1983.10 for month 1 and 10 of 1980.Is it possible that I store them in different format like 1980.1 and 1980.10 for month 1 and 10.

     lat  lon  month      Acre
1  -118.5 48.5 1983.10    1692.9
2  -117.5 48.5 1983.10      11.1
3  -116.5 48.5 1983.10       0.0
4  -115.5 48.5 1983.10       1.1
5  -114.5 48.5 1983.10       0.0
6  -113.5 48.5 1983.10     151.2
7  -112.5 48.5 1983.10       5.0
SaZa
  • 311
  • 2
  • 7
  • 14
  • 1
    If `class(fedfire8004$month)`is `numeric`, then it is too late. You will have to go back to a file or a previous R data that allows to distinguish between january and october. You'll want to split year and month into two different columns or store yyyy.mm as a `character`. You could also replace the `.` with a \ to avoid numeric interpretation. – flodel Oct 20 '13 at 15:56
  • @flodel; no the class is "integer". – SaZa Oct 20 '13 at 16:10
  • can you please show `str(fedfire8004$acres)`? – flodel Oct 20 '13 at 16:14
  • @flodel;num [1:24, 1:18, 1:300] NA NA NA NA NA NA NA NA NA NA ... - attr(*, "dimnames")=List of 3 ..$ lon : chr [1:24] "-124.5" "-123.5" "-122.5" "-121.5" ... ..$ lat : chr [1:18] "31.5" "32.5" "33.5" "34.5" ... ..$ month: chr [1:300] "1980.1" "1980.2" "1980.3" "1980.4" ... – SaZa Oct 20 '13 at 16:17

1 Answers1

2

I think the problem comes from melt applying type.convert to the dim names of your data. To avoid character to numeric conversion, you can replace the . by a -. Try:

dimnames(fedfire8004$acres)$month <- sub("\\.", "-",
                                         dimnames(fedfire8004$acres)$month)

Then apply melt again and you should see 1980-1 and 1980-10.

flodel
  • 87,577
  • 21
  • 185
  • 223
  • Nicely assessed. I think `melt` is being too "aggressive" in its efforts at DWIM-ing if it is automatically coercing dimension attribute types. – IRTFM Oct 20 '13 at 18:40