Reputation: 715
I am reading in a very large dataset using read.table. Once I've read the data in, I want to keep only those records that fall in a certain lat/lon region. So basically I want to define upper and lower bounds on $Lat and $Lon, and discard all other rows. I'm sure this is a fairly easy operation, but I am new to R coding and want to make sure I find the most elegant approach. Here is my code to read in the data:
#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")]
}
Instead of that last part where I keep all track points, I'd like to only keep lat between 30 and 35, and lon between 128 and 140.
Upvotes: 0
Views: 8735
Reputation: 4216
I think what you're looking for is row-selection rather than row-deletion. You can select rows by using AND gate logic with your four criteria:
# 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)
This tells your data frame to select rows that meet all of the four criteria.
Update: As requested, here is the same code in 'subset' form:
subset(track.data, Lat >= 30 & Lat <=35 & Lon >=128 & Lon <= 140)
Upvotes: 3