【问题标题】:Applying a function over a list containing subsetted data in R在包含 R 中子集数据的列表上应用函数
【发布时间】:2020-05-12 13:22:11
【问题描述】:

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

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)

任何帮助将不胜感激!

【问题讨论】:

  • 嗨 Sam,unmarkedFrameOccu 的功能是什么?另外,什么是“未标记的数据框”?
  • 当您回答时,您提供的示例数据是一个小标题而不是一个列表。这对你重要吗?用 tibble 做你想做的事可能更容易。
  • 也考虑一下,by:species_df_list &lt;- by(data_SpeciesExample, data_SpeciesExample$Species_name, myfunction)
  • 嗨,Ian,“未标记”包裹需要 unmarkedFrameOccu 才能计算入住率。我认为它只是改变格式以适应分析。是的,它是一个小问题并不重要。谢谢
  • 很高兴我们能够提供帮助。作为一点反馈,在未来,通过在您的问题中包含 library(unmarked) 来识别软件包会很有帮助。

标签: r list function


【解决方案1】:

这是一种使用基础 R 中的 split 来制作列表并使用 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-12
    • 1970-01-01
    • 1970-01-01
    • 2014-08-05
    • 2019-07-13
    • 1970-01-01
    • 2019-01-14
    相关资源
    最近更新 更多