Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 循环遍历列中的类别,并仅对这些记录执行插值_R_Loops_Gis_Interpolation - Fatal编程技术网

R 循环遍历列中的类别,并仅对这些记录执行插值

R 循环遍历列中的类别,并仅对这些记录执行插值,r,loops,gis,interpolation,R,Loops,Gis,Interpolation,我有一个数据帧,带有采样位置。 我想为每个独特的物种选择数据,所以循环所有独特的物种名称,并为每个物种创建一个插值层。然后按物种名称命名结果。插值部分工作得很好…我只是不知道如何循环遍历每个物种名称并进行命名…。。 我已经粘贴了下面的工作代码,用于选择一个物种名称并创建插值层 SP_NAME sno swgt latdd londd 1 ILLEX ILLECEBROSUS 33

我有一个数据帧,带有采样位置。 我想为每个独特的物种选择数据,所以循环所有独特的物种名称,并为每个物种创建一个插值层。然后按物种名称命名结果。插值部分工作得很好…我只是不知道如何循环遍历每个物种名称并进行命名…。。 我已经粘贴了下面的工作代码,用于选择一个物种名称并创建插值层

                          SP_NAME        sno       swgt    latdd     londd
1              ILLEX ILLECEBROSUS 33.7542857 2.94582857 43.28667 -60.99367
2        CHLOROPHTHALMUS AGASSIZI 13.2971429 0.09205714 43.28667 -60.99367
3              ILLEX ILLECEBROSUS  0.9657143 0.16417143 43.94750 -58.72417
4              ZOARCES AMERICANUS  0.9657143 0.02897143 43.94750 -58.72417
5               AMBLYRAJA RADIATA  2.0457143 1.00240000 43.86483 -59.19717
6 MYOXOCEPHALUS OCTODECEMSPINOSUS  1.0228571 0.10228571 43.86483 -59.19717


setwd("C:/Michelle/Michelle/R/WCTS/Boundaries")
strata <- readOGR(".", "SurveyStrataWGS84")
strata<-spTransform(strata,CRS("+proj=utm +zone=20 ellps=WGS84"))

es_tows1 <- es_tows[which(es_tows$SP_NAME == "HIPPOGLOSSOIDES PLATESSOIDES"),]

ext = extent(strata)
rb <- raster(ext, ncol=554, nrow=279)
stratar <- rasterize(strata, rb)
plot(stratar)
idw.grid<- rasterToPoints(stratar, spatial=TRUE)
gridded(idw.grid) <- TRUE
proj4string(es_tows1) <- CRS("+proj=utm +zone=20 ellps=WGS84")

idw(log(es_tows1$swgt+ 0.00001) ~1 , es_tows1, idw.grid)

pal <- colorRampPalette(rev(brewer.pal(11, "Spectral")))(100)
spplot(idw.out, "var1.pred", col.regions=pal)
SP_NAME sno swgt latdd londd
1 ILLEX ILLECEBROSUS 33.7542857 2.94582857 43.28667-60.99367
2阿加西叶绿素藻13.2971429 0.09205714 43.28667-60.99367
3 ILLEX ILLECEBROSUS 0.9657143 0.16417143.94750-58.72417
4美国佐尔斯0.9657143 0.02897143 43.94750-58.72417
5辐射安布里拉贾2.0457143 1.002400000 43.86483-59.19717
6八棘肌头鱼1.0228571 0.10228571 43.86483-59.19717
setwd(“C:/Michelle/Michelle/R/WCTS/bounders”)
地层<代码>库(rgdal)
图书馆(光栅)

地层
对于(独特的物种(df$SP_NAME)){…}
谢谢,这太棒了。它工作得非常好,我很欣赏您在代码的第一部分中所发现的效率。哦,还有一个快速问题@RobertH,我如何将.tif文件扩展名添加到这一行的sp变量末尾
writerater(光栅(out),filename=sp,datatype='GTiff',overwrite=TRUE)
您不需要指定
datatype='GTiff'
,但可以执行
filename=paste0(sp,'.tif')
library(rgdal)
library(raster)
strata <- readOGR(".", "SurveyStrataWGS84")
strata <- spTransform(strata,CRS("+proj=utm +zone=20 ellps=WGS84"))
ext <- extent(strata)
rb <- raster(ext, ncol=554, nrow=279)
# I think this is all you need to do here 
idw.grid <- as(rb, 'SpatialGrid')

# list of species
species <- unique(es_tows$SP_NAME)

for (sp in species) {
    es_tows1 <- es_tows[es_tows$SP_NAME == sp, ]
    out <- idw(log(es_tows1$swgt+ 0.00001) ~1 , es_tows1, idw.grid)
    # ... save the results ....
    writeRaster(raster(out), filename=sp)
}