0

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.

Kelsey Spencer
  • 57
  • 1
  • 1
  • 9
  • There are multiple ways (functions and packages) for working with spatial data in R so you really need to show your code, preferably with a small data set. Have you read the manual page for the kriging function you are using, especially the description of the output the function returns? – dcarlson Apr 09 '20 at 21:40
  • What is the class of objects in your work environment? Or what is the format that you would like to export?, more information is needed to help you. – rral Apr 09 '20 at 22:02
  • I have edited the question. Please let me know if you need anything else. – Kelsey Spencer Apr 10 '20 at 16:05
  • Yes, see my answer. It is good that you have added the packages used (although you add too many), but the code is still not reproducible. Do not share your data, just use an example from gstat, as I do below. What is not working for you in my answer? – Robert Hijmans Apr 10 '20 at 16:41
  • the predict function "no applicable method for 'predict' – Kelsey Spencer Apr 10 '20 at 17:35
  • That is odd. It should work if you copy and paste the code into a fresh R session. Perhaps after updating packages. – Robert Hijmans Apr 10 '20 at 19:39

1 Answers1

2

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)
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63