0

I have a list dataframes of 288 ASCII data frames in R that contain values and their coordinates. The data are for the average temperature of every hour by month, so the data frames have titles that range from jan_01 to dec_24. What I want to do is restrict these so they are reduced to only containing values for a specific range (region) of coordinates. I can do this successfully for each individual frame using lower bound xl and upper bound xu. For example, the x-coordinates for 01:00 in April would be restricted by using

apr_01 <- apr_01[apr_01$x <= xu,]
apr_01 <- apr_01[apr_01$x >= xl,]

I suspect there's some way to use lapply() or a series of loops so that this operation can be done to all data frames for the whole year, but I couldn't figure out how to implement it since my method above needs the unique data frame name. I tried writing a generalizable function for use with lapply(), but I'm new to R so haven't had much luck. This is probably a trivial problem, but any help would be appreciated!

edit: The function I tried to write, which obviously won't work, was something like

restrict <- function(f){
  f <- f[f$s1 <= xu]
  f <- f[f$s1 >= xl]
}

for(i in 1:24){
  apr_[i] <- restrict(apr_[i])
}
ndem763
  • 320
  • 1
  • 11
  • Please consider reading up on [ask] and how to produce a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). An approach could be a custom function using lapply. – Heroka Nov 01 '15 at 20:58

1 Answers1

1

This turned out to be really simple. Here is the code that solved my problem:

restrict <- function(f){  
  f <- f[f$x <= xu,]
  f <- f[f$x >= xl,]
}

dataframes <- lapply(dataframes, restrict)

The restrict function restricts the x-coordinates to what I'm interested in, then I applied it to each data frame in the list with lapply().

ndem763
  • 320
  • 1
  • 11