SaZa
SaZa

Reputation: 311

reshape a matrix in r

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?

Upvotes: 0

Views: 248

Answers (3)

Ananta
Ananta

Reputation: 3711

library(reshape2)

dcast(mat, id.var=NULL,formula = month+lon ~ lat, value.var="value")

Upvotes: 0

Jilber Urbina
Jilber Urbina

Reputation: 61214

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

Upvotes: 1

IRTFM
IRTFM

Reputation: 263481

> 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

Upvotes: 1

Related Questions