Search code examples
rgridtidyrreshape2melt

Melt grid data in TidyR


I have a very simple data-set that is organized as a grid/raster/table:

1,2,3
4,5,6
7,8,9

I'd like to read it in tidyR (and cognates) into:

x,y,value
1,1,1
1,2,2
1,3,3
.....

It seems like a straightforward problem but I can't figure out how to attack it.


Solution

  • Here is one approach:

    library(raster)
    r <- raster(matrix(1:9, nrow=3, byrow=T))
    cbind(rowColFromCell(r, 1:ncell(r)), values(r))