Christopher Kavazos
Christopher Kavazos

Reputation: 162

R: Using values from a data.frame to subset elements of a list using lapply

I have a list containing annual species abundance data for different wetlands (each element of the list is a different wetland). The elements have columns = species and rows = survey year. I want to subset each element differently, using the lapply function, to grab 3 years of data defined by a separate data.frame. See below code for generating the data:

set.seed(1)
df <- data.frame(year=c("x00", "x01", "x02", "x03", "x04", "x05"), 
                               mean=c(12, 10, 13, 10, 9, 11), 
                               sd=c(1, 2, 3, 1, 3, 2))
normv <- function( n , mean , sd ){
        out <- rnorm( n*length(mean) , mean = mean , sd = sd )
        return( matrix( out , , ncol = n , byrow = FALSE ) )
        }
com1 <- round(normv( 10 , df$mean , df$sd ),0)
row.names(com1) <- df$year
colnames(com1) <- c("sp1", "sp2", "sp3", "sp4", 
    "sp5", "sp6", "sp7", "sp8", "sp9", "sp10")

com2 <- round(normv( 10 , df$mean , df$sd ),0)
row.names(com2) <- df$year
colnames(com2) <- c("sp1", "sp2", "sp3", "sp4", "sp5", 
    "sp6", "sp7", "sp8", "sp9", "sp10")

com3 <- round(normv( 10 , df$mean , df$sd ),0)
row.names(com3) <- df$year
colnames(com3) <- c("sp1", "sp2", "sp3", "sp4", "sp5", 
    "sp6", "sp7", "sp8", "sp9", "sp10")

com.list <- list(com1, com2, com3)

years <- data.frame(rbind(c("x00", "x01", "x02"), c("x03", "x04", "x05"), 
    c("x02", "x03", "x05")))
colnames(years) <- c("com1", "com2", "com3")

I have been trying something like:

library(data.table)
lapply(com.list, function(x){
    x.sub <- data.table::setDT(data.frame(x))[
        row.names(x) %chin% as.character(years[,x])]
    return(x.sub)
}

Any help would be much appreciated! Chris

Upvotes: 2

Views: 493

Answers (1)

akrun
akrun

Reputation: 887971

If we are looking to subset based on corresponding 'years' column, then we can use Map from base R

Map(function(x, y) subset(x, row.names(x) %in% y), com.list, years)

If we want to use lapply, then loop through the sequence of the list and then subset the 'years' and the 'com.list' based on that

lapply(seq_along(com.list), function(i) {
          dat <- com.list[[i]]
          subset(dat, row.names(dat) %in% years[,i])
 })

A tidyverse option would be

library(tidyverse)
map2(com.list, years, ~  .x %>%
                           as.data.frame %>% 
                           filter(row.names(.) %in% .y))

Upvotes: 2

Related Questions