Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R中包含子集数据的列表上应用函数_R_List_Function - Fatal编程技术网

在R中包含子集数据的列表上应用函数

在R中包含子集数据的列表上应用函数,r,list,function,R,List,Function,我创建了一个列表,该列表按物种名称过滤数据集。我想用一个函数来改变列表中每个亚组物种的形式,而不是单独做每一个。这是数据的简化版本,作为示例 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

我创建了一个列表,该列表按物种名称过滤数据集。我想用一个函数来改变列表中每个亚组物种的形式,而不是单独做每一个。这是数据的简化版本,作为示例

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
col.filters列表
我想做的是在整个列表上应用一个函数,为每个物种返回一个数据框(未标记的数据框)。这是一次只处理一个物种的代码,我想将其应用于整个数据集:

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)
P.pica%
总结(
检测=1
))

P.pica_ocu$Photo.Date

这里有一种方法,它使用

split
从基R开始创建列表,并
purr
将函数应用于每个列表元素:

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

嗨,山姆,功能
未标记框架
应该做什么?还有,什么是“未标记的数据帧”?当您回答时,您提供的示例数据是TIBLE而不是列表。这对你有关系吗?用TigBLE做你想做的事情可能更容易。考虑一下,< <代码> > <代码>:<代码>伊恩。我认为它只是改变了格式以适应分析。是的,这不重要,它是一个tibble。谢天谢地,我们能帮忙。作为一点反馈,将来在您的问题中包括
library(unmarked)
将有助于识别包。