Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#list all files in the folder

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

#make a function to extract data from Uni_Del files
read_udel = function(p){
  d = read.fwf(p, widths=rep(8,15))
  names(d) = c("long","lat",month.abb,"mean")
  return(d)
}

#Make a list of that contains all data
read.all = lapply(p, read_udel)

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[[1]]

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[1], lat_range[2]) &
                           between(x$lon, lon_range[1], lon_range[2]),])

Or entirely in base R:

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

Output (for either approach):

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

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

[[3]]
  lat lon
2   2  22
3   3  23
4   4  24
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading