无需在R中使用FOR循环即可读取具有不规则格式的文本文件的有效方法

时间:2013-06-30 01:47:06

标签: r loops text

我有一个输入文件,其格式如下: - 前两行包含模型和参数的参数。月&年:我只需要从这2行获得月份和年份 - 下一个块包含实际数据:172行和256列 数据持续N次

Link to the actual data

另一个文件是ArcGIS ascii网格,其前6行是ArcGIS参数,172行x 256列(区域内等于1,其他地方为-9999)

Link to the ArcGIS ascii grid

我想要做的是读取数据并根据遮罩网格计算每个时间步的平均值。目前,我能想到的唯一方法是使用3个嵌套循环

for (i in 1:N) {
   for (j in 1:172) {
      for (k in 1:252) {
           Read the data
           Do calculation
           Write the result out
      }
   }
{

有没有更好的方法可以避免使用这种复杂的FOR循环?任何建议将不胜感激!

编辑:根据agstudy建议和其他来源,我逐行读取我的数据并使用strsplit来获取值。我的最终代码如下:

########################## Read the grid first ########################
con <- file(gisGrid) 
open(con);

# Read the first 6 lines
gisData <- readLines(textConnection(
'ncols         256
nrows         172
xllcorner     730000
yllcorner     227000
cellsize      1320
NODATA_value  -9999'),n=6)

# Extract value
gisPara <- matrix(unlist(strsplit(gisData,' +')), ncol=2, byrow=TRUE)
summary(gisPara)

# Read the mask grid:
gridValue <- read.table(file = gisGrid, header = FALSE, skip=6)
gridValueVec <- as.vector(as.matrix(gridValue))

close(con)

######################### Read the data file #########################
con <- file(dataFile) 
open(con);

# Define number of timesteps, nRow, nCol
nTime <- 3
nRow <- 172
nCol <- 256

# Read the whole file in
data.lines <- scan(con, what=character(), sep='\n')
data <- NULL
# Create 3D object to store data for each timestep
data$timestep <- list( rep( matrix(nrow=nRow, ncol=nCol), nTime ) )
data$month <- list( rep( nTime ) )
data$year <- list( rep( nTime ) )
data$multiply <- list( rep( nTime ) )

# Loop over all timestep
for (i in 1:nTime) {
  # Read the first 2 lines
  data.lines <- data.lines[-1] # remove line from the dataset

  data.line2 <- strsplit(data.lines[1],' ')
  data$month[[i]] <- data.line2[[1]][24]
  data$year[[i]] <- as.numeric(data.line2[[1]][25])
  data.lines <- data.lines[-1]

  dataRead <- matrix(nrow=nRow, ncol=nCol)
  for(j in 1:nRow) 
  {
    dataRead[j,] <- as.numeric(strsplit(data.lines[1],' ')[[1]])
    data.lines <- data.lines[-1]
  }         

  # Multiply data with the mask grid
  dataReadVector <- as.vector(dataRead)
  gridMultiplication <- ifelse(gridValueVec==-9999, NA,
                               dataReadVector * gridValueVec )

  data$multiply[[i]] <- mean(gridMultiplication, na.rm=TRUE)
  data$timestep[[i]] <- dataRead
}

close(con)

1 个答案:

答案 0 :(得分:0)

对于您可以使用的第一个文件,请执行以下操作:

使用readLines

解析您只阅读2行的日期
dd <- readLines(
   textConnection('internal  1.0  (free)  -1                 Sep 1990'),n=2)
regmatches(dd,gregexpr('[A-Z][a-z]{2} [0-9]{4}$',dd))[[1]]
"Sep 1990" ## strsplit(...,' ') if you want month/and year separately

然后你用类似的东西读取矩阵:

 read.table(...,skip=2)

对于第二个文件,您可以使用`strsplit来读取文件中的前6行参数:

dd <- readLines(textConnection('ncols         256
nrows         172
xllcorner     730000
yllcorner     227000
cellsize      1320
NODATA_value  -9999
'),n=6)
matrix(unlist(strsplit(dd,' +')),ncol=2,byrow=TRUE)

    [,1]           [,2]    
[1,] "ncols"        "256"   
[2,] "nrows"        "172"   
[3,] "xllcorner"    "730000"
[4,] "yllcorner"    "227000"
[5,] "cellsize"     "1320"  
[6,] "NODATA_value" "-9999"