# Subset across multiple data frames contained in a large list object of R

I have a list of data frames contained in a large list object of R. In each data frame of the list I have latitude and longitude columns. I want to subset all the data frames in contained the list to certain latitude and longitude coordinates. How can I don this in R?

I tried sub setting and map method but none worked for list object. However if I changed list of objects into data frame I was able to do that. But since I have so many years I can’t go about this way.

Here is code how I am creating list object:

``````#list all files in the folder

p <- paste0("air_temp", sep= ".", 1901:1991)

#make a function to extract data from Uni_Del files
names(d) = c("long","lat",month.abb,"mean")
return(d)
}

#Make a list of that contains all data
``````

latitude and longitude range I need:

``````typelon_range <- c(-90, -70)
lat_range <- c(25, 45) here
``````

This is how did for year 1990 data frame:

``````data1990= read.all[]

df_sub = subset(data1990, data1990\$long >=-90 &data1990\$long <=-70 & data1990\$lat >=45 & data1990\$lat >=25)
``````

could any one suggest me a way in which how can I subset list objects in R?

### >Solution :

You can use `lapply()` and `dplyr::between()` (or base R, see below) to subset your list:

``````df1 <- df2 <- df3 <- data.frame(lat = 1:10, lon = 21:30) # Example data frames
ll <- list(df1, df2, df3) # example list

lat_range <- c(2,4) # example ranges
lon_range <- c(22,29)

library(dplyr)

lapply(ll, function(x) x[between(x\$lat, lat_range, lat_range) &
between(x\$lon, lon_range, lon_range),])

``````

Or entirely in base R:

``````lapply(ll, function(x) x[(x\$lat >= lat_range & x\$lat <= lat_range) &
(x\$lon >= lon_range & x\$lon <= lon_range),])

``````

Output (for either approach):

``````[]
lat lon
2   2  22
3   3  23
4   4  24

[]
lat lon
2   2  22
3   3  23
4   4  24

[]
lat lon
2   2  22
3   3  23
4   4  24
``````