0

I have this matrix(head and tail is given below), and it has lat ,lon and variable for USA for a time period starts from January 1948 to December 2004:

  # head of matrix
  lon  lat  month   value
1 -124.5 31.5 1980.1    NA
2 -123.5 31.5 1980.1    NA
3 -122.5 31.5 1980.1    NA
4 -121.5 31.5 1980.1    NA
5 -120.5 31.5 1980.1    NA
6 -119.5 31.5 1980.1    NA

# tail of matrix  
           lon  lat   month   value
[129595,] -106.5 48.5 2004.12     0
[129596,] -105.5 48.5 2004.12     0
[129597,] -104.5 48.5 2004.12    71
[129598,] -103.5 48.5 2004.12    NA
[129599,] -102.5 48.5 2004.12    NA
[129600,] -101.5 48.5 2004.12    NA

I want to reshape it into this form which is for each month of the year for example this is for January 1980:

lon....>    31.5       32.5       33.5    ....     48.5
lat -101.5 value11   value12     value13
 .  -102.5 ....
 .  -103.5
 .    .
 \/   .                          value ii

Is there any way I can do that?

SaZa
  • 311
  • 2
  • 7
  • 14
  • 1
    possible duplicate of [Reshape three column data frame to matrix](http://stackoverflow.com/questions/9617348/reshape-three-column-data-frame-to-matrix) – Frank Oct 08 '13 at 20:37

3 Answers3

1
> dat <- read.table(text="lon  lat  month   value
+  -124.5 31.5 1980.1    2
+  -123.5 31.5 1980.1    3
+  -122.5 31.5 1980.1    4
+  -121.5 31.5 1980.1    5
+  -120.5 31.5 1980.1    6
+  -119.5 31.5 1980.1    7
+  -106.5 48.5 2004.12     0
+  -105.5 48.5 2004.12     0
+  -104.5 48.5 2004.12    71
+  -103.5 48.5 2004.12    8
+  -102.5 48.5 2004.12    9
+  -101.5 48.5 2004.12    0", header=TRUE)
> xtabs(value~lat+lon+month, data=dat, exclude="")
, , month = 1980.1

      lon
lat    -124.5 -123.5 -122.5 -121.5 -120.5 -119.5 -106.5 -105.5 -104.5 -103.5
  31.5      2      3      4      5      6      7      0      0      0      0
  48.5      0      0      0      0      0      0      0      0      0      0
      lon
lat    -102.5 -101.5
  31.5      0      0
  48.5      0      0

, , month = 2004.12

      lon
lat    -124.5 -123.5 -122.5 -121.5 -120.5 -119.5 -106.5 -105.5 -104.5 -103.5
  31.5      0      0      0      0      0      0      0      0      0      0
  48.5      0      0      0      0      0      0      0      0     71      8
      lon
lat    -102.5 -101.5
  31.5      0      0
  48.5      9      0
IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • Thanks for your answer. I don't know why it doesn't work with my data when I just copy -past all the data between those commands line it doesn't works because it doesn't show the space between values and other variables when it runs. this is the error message:Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, : line 2 did not have 4 elements – SaZa Oct 09 '13 at 15:52
1

dcast from reshape2 is your friend in this situation. Suppose your data.frame is called df

> library(reshape2)
> dcast(df, lat + month ~lon)
   lat  month -124.5 -123.5 -122.5 -121.5 -120.5 -119.5
1 31.5 1980.1     NA     NA     NA     NA     NA     NA

Let's replace NA with some random values to see how it works

> set.seed(1)
> df[,4 ] <- sample(20:60, 6)
> df  # this is how the new df looks like 
     lon  lat  month value
1 -124.5 31.5 1980.1    30
2 -123.5 31.5 1980.1    34
3 -122.5 31.5 1980.1    42
4 -121.5 31.5 1980.1    54
5 -120.5 31.5 1980.1    27
6 -119.5 31.5 1980.1    52

> dcast(df, lat + month ~lon)  # here's the job done by `dcast`
   lat  month -124.5 -123.5 -122.5 -121.5 -120.5 -119.5
1 31.5 1980.1     30     34     42     54     27     52
Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138
0
library(reshape2)

dcast(mat, id.var=NULL,formula = month+lon ~ lat, value.var="value")
Ananta
  • 3,671
  • 3
  • 22
  • 26