簡體   English   中英

在包含 R 中的子集數據的列表上應用 function

[英]Applying a function over a list containing subsetted data in R

我創建了一個按物種名稱過濾數據集的列表。 我想使用 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-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

我想做的是在整個列表上應用 function 以返回每個物種的數據框(未標記的數據框)。 這是一次只用於一個物種的代碼,我想將其應用於整個數據集:

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)

任何幫助將非常感激!

這是一種方法,它使用從基礎purrrsplit來制作列表,然后將函數應用於每個列表元素:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM