我正在使用 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 之间。