我有一份清单,其中包含不同湿地的年度物种丰度数据(清单中的每个元素都是不同的湿地)。元素有列=种类和行=调查年份。我想使用lapply函数以不同方式对每个元素进行子集化,以获取由单独的data.frame定义的3年数据。请参阅以下用于生成数据的代码:
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")
我一直在尝试这样的事情:
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)
}
任何帮助将不胜感激! 克里斯
答案 0 :(得分:2)
如果我们根据相应的“年”列查看子集,那么我们可以使用Map
中的base R
Map(function(x, y) subset(x, row.names(x) %in% y), com.list, years)
如果我们想要使用lapply
,那么循环遍历list
的序列,然后将'years'和'com.list'基于该
lapply(seq_along(com.list), function(i) {
dat <- com.list[[i]]
subset(dat, row.names(dat) %in% years[,i])
})
tidyverse
选项
library(tidyverse)
map2(com.list, years, ~ .x %>%
as.data.frame %>%
filter(row.names(.) %in% .y))