r - 在包含 R 中子集数据的列表上应用函数
问题描述
我创建了一个按物种名称过滤数据集的列表。我想使用函数而不是单独执行每个子集来更改列表中每个子集物种的形式。这是一个简化版本的数据作为例子。
structure(list(Camera.Trap.Name = structure(c(5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L), .Label = c("CT-Tst-1-1", "CT-Tst-2-1",
"CT-Tst-3-1", "CT-Tst-4-1", "CT-Tst-5-1", "CT-Tst-6-1", "CT-Tst-8-1"
), class = "factor"), Sampling.Event = structure(c(5L, 5L, 5L,
5L, 5L, 5L, 7L, 7L, 7L, 7L), .Label = c("Olney 1", "Olney 2",
"Olney 3", "Olney 4", "Olney 5", "Olney 6", "Olney 7"), class = "factor"),
Photo.Date = structure(c(67L, 67L, 68L, 68L, 70L, 70L, 72L,
72L, 73L, 73L), .Label = c("2018-03-26", "2018-03-27", "2018-03-28",
"2018-03-29", "2018-04-12", "2018-04-13", "2018-04-14", "2018-04-15",
"2018-04-16", "2018-04-17", "2018-04-18", "2018-04-19", "2018-04-20",
"2018-04-21", "2018-04-22", "2018-04-23", "2018-04-24", "2018-04-25",
"2018-04-26", "2018-04-27", "2018-04-28", "2018-04-29", "2018-04-30",
"2018-05-01", "2018-05-02", "2018-05-03", "2018-05-04", "2018-05-05",
"2018-05-06", "2018-05-07", "2018-05-08", "2018-05-09", "2018-05-10",
"2018-05-11", "2018-05-12", "2018-05-14", "2018-05-15", "2018-05-16",
"2018-05-17", "2019-11-12", "2019-11-13", "2019-11-14", "2019-11-15",
"2019-11-16", "2019-11-17", "2019-11-18", "2019-11-20", "2019-11-21",
"2019-11-22", "2019-12-13", "2019-12-19", "2019-12-20", "2020-03-24",
"2020-03-25", "2020-03-26", "2020-03-27", "2020-03-28", "2020-03-29",
"2020-03-30", "2020-03-31", "2020-04-01", "2020-04-02", "2020-04-03",
"2020-04-04", "2020-04-05", "2020-04-06", "2020-04-07", "2020-04-08",
"2020-04-09", "2020-04-10", "2020-04-11", "2020-04-22", "2020-04-23",
"2020-04-24", "2020-04-25", "2020-04-28", "2020-04-29", "2020-04-30",
"2020-05-01", "2020-05-02", "2020-05-03", "2020-05-04", "2020-05-05",
"2020-05-06", "2020-05-07"), class = "factor"), Species_name = c("Cygnus olor",
"Cygnus olor", "Cygnus olor", "Cygnus olor", "Cygnus olor",
"Cygnus olor", "Pica pica", "Pica pica", "Pica pica", "Pica pica"
)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
))
然后我继续按每个物种对数据进行子集化:
col.filters <- unique(data_SpeciesExample$Species_name)
lapply(seq_along(col.filters), function(x) {
filter(data_SpeciesExample, Species_name == col.filters[x])
}
) -> list
我想做的是在整个列表上应用一个函数来为每个物种返回一个数据框(未标记的数据框)。这是一次只用于一个物种的代码,我想将其应用于整个数据集:
P.pica <- list$`Pica pica`
(P.pica_Occu <- P.pica %>%
group_by(Sampling.Event, Photo.Date) %>%
summarise(
Detection= 1
))
P.pica_Occu$Photo.Date <- as.factor(P.pica_Occu$Photo.Date)
(P.pica_Occu_Wide <- pivot_wider(P.pica_Occu, names_from = Photo.Date, values_from = Detection))
P.pica_Occu_Wide[is.na(P.pica_Occu_Wide)] <- 0
Unmark_P.pica<- unmarkedFrameOccu(y =P.pica_Occu_Wide)
任何帮助将非常感激!
解决方案
这是一种使用split
基础 R 来制作列表并将purrr
函数应用于每个列表元素的方法:
library(dplyr)
library(purrr)
library(tidyr)
data_SpeciesExample %>%
split(.$Species_name) %>%
map(~ group_by(.,Sampling.Event,Photo.Date) %>%
summarize(Detection = 1) %>%
pivot_wider(names_from = Photo.Date, values_from = Detection) %>%
mutate_at(vars(-Sampling.Event), list(~replace_na(.,0))) %>%
as.data.frame
)
#$`Cygnus olor`
# Sampling.Event 2020-04-07 2020-04-08 2020-04-10
#1 Olney 5 1 1 1
#$`Pica pica`
# Sampling.Event 2020-04-22 2020-04-23
#1 Olney 7 1 1