0

我正在使用 read.table 读取一个非常大的数据集。一旦我读入了数据,我只想保留那些落在某个纬度/经度区域的记录。所以基本上我想定义 $Lat 和 $Lon 的上限和下限,并丢弃所有其他行。我确信这是一个相当简单的操作,但我是 R 编码的新手,想确保找到最优雅的方法。这是我读取数据的代码:

#trackfilenums=1:96   #these are the legs of the track files
trackfilenums=1

for(i in trackfilenums){

print(paste(i, 96, Sys.time(), sep=" : "))
track.data.file <- paste('input/track_param/track_param_',zpad(i,4),'.txt', sep="")
track.data <- read.table( track.data.file ,fill=TRUE,as.is=TRUE,skip=1)
sel <- !is.na(track.data[,9])
track.data <- track.data[sel,]
colnames(track.data) <- c("ID", "Hr", "Lon", "Lat", "UT", "VT", "LandFlag", "ET", "CP", "Penv", "Rmax", "Vmax", "X1", "N", "EOF1", "EOF2", "EOF3", "EOF4", "Angle", "MPI", "Filling.alpha", "Filling.beta", "Filling.gamma")

## keep all points 
track.data<-track.data[,c("ID", "Hr", "Lon", "Lat", "UT", "VT", "LandFlag", "ET", "CP", "Penv", "Rmax", "Vmax", "X1")]

}   

与我保留所有跟踪点的最后一部分不同,我只想将 lat 保持在 30 到 35 之间,将 lon 保持在 128 到 140 之间。

4

1 回答 1

3

我认为您正在寻找的是行选择而不是行删除。您可以使用 AND 门逻辑和您的四个条件来选择行:

# Note that I am using the 'attach' function to
# make things a little easier to read.
attach(track.data)
track.data[Lat >= 30 & Lat <=35 & Lon >=128 & Lon <= 140,]
detach(track.data)

这告诉您的数据框选择满足所有四个条件的行。

更新:根据要求,这里是“子集”形式的相同代码:

subset(track.data, Lat >= 30 & Lat <=35 & Lon >=128 & Lon <= 140)
于 2013-05-22T16:39:59.100 回答