嵌套for循环以提取数据

时间:2019-09-25 17:23:50

标签: r

我有一个名为coord_pts的列表,其中包含4个SpatialPointsDataFrame文件和一个具有14个栅格的列表。我想要的是从14个栅格中提取数据,以获取每个SpatialpointsDataFrame文件的坐标。我试图做一个嵌套的for循环,但是我只得到coord_pts的第一个对象的输出。

crops<-list()
extract<-list() 
 for(j in 1:length(coord_pts)){
  for(i in 1:length(Temp_raster)){
   extract[[j*i]]<-extract(Temp_raster[[i]],coord_pts[[j]], method='simple')
   crops[[j]]<-data.frame(c(extract))
  }  
   colnames(crops)<-c('MeanTemp', 'Prec', 'Temp1', 'Temp10', 'Temp11', 'Temp12', 'Temp2', 'Temp3', 'Temp4', 'Temp5', 'Temp6', 'Temp7', 'Temp8', 'Temp9')      
   }

3 个答案:

答案 0 :(得分:0)

递归的基础,而又不了解您的数据集。

----- begin MyProcessingApp.app/Contents/Info.plist ------- snip ------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>CFBundleExecutable</key>
        <string>MyAudioProcessingApp</string>
        <key>CFBundleGetInfoString</key>
        <string>Created by Qt/QMake</string>
        <key>CFBundleIconFile</key>
        <string>vcore.icns</string>
        <key>CFBundleIdentifier</key>
        <string>com.mycompany.MyAudioProcessingApp</string>
        <key>CFBundlePackageType</key>
        <string>APPL</string>
        <key>CFBundleSignature</key>
        <string>????</string>
        <key>LSMinimumSystemVersion</key>
        <string>10.10</string>
        <key>NOTE</key>
        <string>This file was generated by Qt/QMake.</string>
        <key>NSMicrophoneUsageDescription</key>
        <string>To allow MyAudioProcessingApp to process incoming audio data.</string>
        <key>NSPrincipalClass</key>
        <string>NSApplication</string>
        <key>NSSupportsAutomaticGraphicsSwitching</key>
        <true/>
</dict>
</plist>
----- end MyProcessingApp.app/Contents/Info.plist ------- snip ------

---- begin /Library/LaunchDaemons/com.mycompany.myprogram.plist   ------ snip ------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "
http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>EnvironmentVariables</key>
    <dict>
      <key>PATH</key>
      <string>/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:</string>
    </dict>
    <key>Label</key>
    <string>com.mycompany.MyAudioProcessingApp</string>
    <key>Program</key>
    <string>/Library/MyCompany/MyAudioProcessingApp/run_my_program_in_background.sh</string>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <true/>
    <key>StandardOutPath</key>
    <string>/tmp/myprogram.stdout</string>
    <key>StandardErrorPath</key>
    <string>/tmp/myprogram.stderr</string>
    <key>UserName</key>
    <string>jaf</string>      // NOTE: this is set dynamically to the correct user as part of the install-as-service step
    <key>ProcessType</key>
    <string>Interactive</string>
    <key>GroupName</key>
    <string>admin</string>
    <key>InitGroups</key>
    <true/>
  </dict>
</plist>
---- end /Library/LaunchDaemons/com.mycompany.myprogram.plist   ------ snip ------

---- begin /Library/MyCompany/MyAudioProcessingApp/run_my_program_in_background.sh   ------ snip ------
#!/bin/bash
PATH_TO_MYPROGRAM_EXE="/Library/MyCompany/MyAudioProcessingApp/MyAudioProcessingApp.app/Contents/MacOS/MyAudioProcessingApp"
"$PATH_TO_MYPROGRAM_EXE" run_without_gui
exit 0
---- end /Library/MyCompany/MyAudioProcessingApp/run_my_program_in_background.sh   ------ snip ------

想法是您编写一个满足您需要的函数,然后将列表传递给is.deep <- function(x){ is.list(x) | is.data.frame(x) } list.dive <- function(L, func = NULL, ...){ L <- func(L, ...) if(!is.deep(L)){ L }else{ lapply(L,function(x){ list.dive(x, func, ...) }) } } ,然后将该函数传递给L。该函数将执行,测试结果是否仍然是您可以迭代的列表或其他类型的对象,它将再次执行该函数,直到不再满足条件为止。

答案 1 :(得分:0)

在您的问题中始终包括一些示例数据。另外,您可能不需要循环,或者至少不需要嵌套循环。这是 R ,您应该假定循环是隐式的。

在最简单的情况下,您可以使用文件创建RasterStack

// resp is an array received from axios.get:

this.$store.commit({
     type: 'setCases',
     items: resp
})

如果由于栅格未对齐而无法创建RasterStack,则可以使用列表,如您的问题;和循环。

library(raster)
s <- stack(system.file("external/rlogo.grd", package="raster"))
coord_pts <- cbind(1:4, 1:4) * 10
extract(s, coord_pts)

#     red green blue
#[1,] 255   255  255
#[2,]  55    55   53
#[3,] 255   255  253
#[4,] 149   159  186

假设每个列表元素都有一个单独的图层,

Temp_raster <- as.list(s)

或更复杂的情况下

m <- matrix(nrow=nrow(coord_pts), ncol=length(Temp_raster))
colnames(m) <- sapply(Temp_raster, names)
for (i in 1:length(Temp_raster)) {
   m[, i] <- extract(Temp_raster[[i]], coord_pts)
}

m
#     red green blue
#[1,]  255  255  255
#[2,]   55   55   53
#[3,]  255  255  253
#[4,]  149  159  186

然后从那里拿走。

答案 2 :(得分:0)

对不起,我没有很好地解释它,取决于坐标集的SpatialPointsDataFrame在世界范围内的40至100个数据点之间,并且是变量(HI)。像这样:

Lat  Long          HI 
 -39.85000  -72.50000  0.4240000     
 -36.75000  142.10000  0.3620000    

...

这14个栅格是全球温度(0.5摄氏度)的年度温度,降水和每月温度的数据。我想从栅格中获取每个带有索引和14个变量的坐标集的数据框。

 Lat    Long    HI      temp        prec    temp1        temp2      temp3   ......
-39.85  -72.5   0.424   10.65100003 2305    15.77600002 15.2840004  13.36400032 
-36.75  142.1   0.362   14.54166676 466     20.79999924 21.1000004  18.59200096