Kelsey Spencer
Kelsey Spencer

Reputation: 69

Exporting Kriged Values from R

Does anyone have any suggestions on how to export the values from your kriged data in R. I have them exported as a raster, but I need the actual cell values. The data that I'm working with are fish densities. I'm kriging the data and then converting to densities to abundance. BUT in order to convert to abundances I need the kriged values in .csv format.

Below is the code that I'm using:

library(sp)
library(gstat)
library(raster)
library(automap)
library(ggplot2)
library(rgdal)
library(maptools)

fsite.fit=fit.variogram(fsite.vario, vgm(model="Sph",psill=fmy.psill,range=fmy.range,nugget=fmy.nugget),
                    fit.method=1)

fXloc = data.frame(Data.krig)$fXloc
fYloc = data.frame(Data.krig)$fYloc
fsite.chull = chull(fXloc,fYloc)
plot(fXloc,fYloc)
lines(fXloc[fsite.chull],fYloc[fsite.chull])

fsite.grid = polygrid(
  xgrid=seq(min(fXloc),max(fXloc),length=100),
  ygrid=seq(min(fYloc),max(fYloc),length=100),
  cbind(
    fXloc[fsite.chull],
    fYloc[fsite.chull]))
names(fsite.grid)=c("fXloc","fYloc")
coordinates(fsite.grid)=c("fXloc","fYloc")
fsite.grid = as(fsite.grid, "SpatialPixels")

plot(fYloc~fXloc,cex=1.2,pch=20,col=2)
points(data.frame(fsite.grid)$fXloc,data.frame(fsite.grid)$fYloc,pch="+")

fsite.ok = krige(fADens~1, Data.krig, fsite.grid, fsite.fit)

Now what I want is to export the fsite.ok within fsite.grid but I am unable to join them in the same data frame. I'm not sure where to go from here. Please KINDLY let me know if you would like to me to add anything else. At this time, I am not allowed to share my data.

Upvotes: 0

Views: 472

Answers (1)

Robert Hijmans
Robert Hijmans

Reputation: 47436

Here is a minimal, reproducible example from gstat. It looks like you are interested in object fsite.ok. Presumably a Spatial*DataFrame.

Example code

library(gstat)
library(sp)
data(meuse)
coordinates(meuse) = ~x+y
data(meuse.grid)
gridded(meuse.grid) = ~x+y
meuse.gstat <- gstat(id = "zinc", formula = zinc ~ 1, data = meuse, 
    nmax = 7, set = list(idp = .5))
meuse.gstat
z <- predict(meuse.gstat, meuse.grid)

Now you can do

d <- data.frame(z)
write.csv(d, "values.csv", row.names=FALSE)

So in your example that would be

write.csv(data.frame(fsite.ok), "values.csv", row.names=FALSE)

Upvotes: 2

Related Questions