0

我有这个功能,我想用 50 个不同的数据集运行它五十 (50) 次。我每次都必须更改第 4、18 和 20 行的索引。(这些是每次运行中唯一改变的参数)。

1  myfunc <- function(file){
2  library(ncdf)
3  setwd("C:\\Users\\Desktop\\data")
4  AMS  <- read.csv("C:\\Users\\Desktop\\data\\myarray.***index***.csv",header=FALSE)
5  
6  xval <- seq(-124.5,-101.5,by=1)
7  yval <- seq(31.5,48.5,by=1)
8  nx <- length(xval)
9  ny <- length(yval)
10 data_temp <- array(NA, dim=c(nx,ny))
11    for( i in 1:nx ){
12    for( j in 1:ny ) {
13        data_temp[i,j] <- (AMS[i,j])
14    }
15    }
16 dimx <- dim.def.ncdf( "lon", "degreesE", xval,unlim=FALSE)
17 dimy <- dim.def.ncdf( "lat", "degreesN", yval,unlim=FALSE)
18 dimt <- dim.def.ncdf( "time", "year", ***index*** , unlim=TRUE)
19 vari <- var.def.ncdf("precipt", "mm/d", list(dimx,dimy,dimt),999999.999 )
20 ncnew <- create.ncdf( "output.***index***.nc", var )
21 put.var.ncdf( ncnew, vari,data_temp, start=c(1,1,1), count=c(nx,ny,1) )
22 close.ncdf(ncnew)
}

有没有直接的方法可以做到这一点?

4

1 回答 1

2

您可以尝试,假设您希望函数将所谓的索引作为参数,

myfunc <- function(file_index){
2  library(ncdf)
3  setwd("C:\\Users\\Desktop\\data")
4  AMS  <- read.csv(sprintf("C:\\Users\\Desktop\\data\\myarray.%d.csv", file_index),header=FALSE)
5  
6  xval <- seq(-124.5,-101.5,by=1)
7  yval <- seq(31.5,48.5,by=1)
8  nx <- length(xval)
9  ny <- length(yval)
10 data_temp <- array(NA, dim=c(nx,ny))
11    for( i in 1:nx ){
12    for( j in 1:ny ) {
13        data_temp[i,j] <- (AMS[i,j])
14    }
15    }
16 dimx <- dim.def.ncdf( "lon", "degreesE", xval,unlim=FALSE)
17 dimy <- dim.def.ncdf( "lat", "degreesN", yval,unlim=FALSE)
18 dimt <- dim.def.ncdf( "time", "year", file_index , unlim=TRUE)
19 vari <- var.def.ncdf("precipt", "mm/d", list(dimx,dimy,dimt),999999.999 )
20 ncnew <- create.ncdf(sprintf("output.%d.nc",file_index), var )
21 put.var.ncdf( ncnew, vari,data_temp, start=c(1,1,1), count=c(nx,ny,1) )
22 close.ncdf(ncnew)
}

接着

lapply(1:50, myfunc)
于 2013-10-23T19:53:28.733 回答