簡體   English   中英

將行合並為一行並合並信息

[英]Combine rows into one row and merge information

我有這些數據

df <- structure(list(Site = c("2B", "2B", "2B", "2B", "2B", "2C", "2C", 
"2C", "2C", "2C", "FS", "FS", "FS", "FS", "HE", "HE", "HE"), 
    Year = c(2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 
    2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014), Maxcount_site = c(46L, 
    46L, 46L, 46L, 46L, 25L, 25L, 25L, 25L, 25L, 19L, 19L, 19L, 
    19L, 10L, 10L, 10L), Status = c("New Capture", "New Capture", 
    "Retrap", "Retrap", "Retrap", "New Capture", "New Capture", 
    "Retrap", "Retrap", "Retrap", "New Capture", "New Capture", 
    "Retrap", "Retrap", "New Capture", "New Capture", "Retrap"
    ), Name = c("bluti", "greti", "bluti", "greti", "marti", 
    "bluti", "greti", "bluti", "greti", "marti", "bluti", "greti", 
    "bluti", "greti", "bluti", "greti", "bluti"), maxcount = c(17L, 
    3L, 14L, 11L, 1L, 2L, 2L, 13L, 5L, 3L, 7L, 1L, 9L, 2L, 5L, 
    1L, 4L), blutinew = c(17L, NA, NA, NA, NA, 2L, NA, NA, NA, 
    NA, 7L, NA, NA, NA, 5L, NA, NA), blutiretrap = c(NA, NA, 
    14L, NA, NA, NA, NA, 13L, NA, NA, NA, NA, 9L, NA, NA, NA, 
    4L), gretinew = c(NA, 3L, NA, NA, NA, NA, 2L, NA, NA, NA, 
    NA, 1L, NA, NA, NA, 1L, NA), gretiretrap = c(NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_), martinew = c(NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_), martiretrap = c(NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_)), class = c("grouped_df", "tbl_df", "tbl", "data.frame"
), row.names = c(NA, -17L), groups = structure(list(Site = c("2B", 
"2C", "FS", "HE"), .rows = structure(list(1:5, 6:10, 11:14, 15:17), ptype = integer(0), class = c("vctrs_list_of", 
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -4L), .drop = TRUE))

head(df)
Site   Year Maxcount_site Status      Name  maxcount blutinew blutiretrap gretinew gretiretrap martinew martiretrap
  <chr> <dbl>         <int> <chr>       <chr>    <int>    <int>       <int>    <int>       <int>    <int>       <int>
1 2B     2014            46 New Capture bluti       17       17          NA       NA          NA       NA          NA
2 2B     2014            46 New Capture greti        3       NA          NA        3          NA       NA          NA
3 2B     2014            46 Retrap      bluti       14       NA          14       NA          NA       NA          NA
4 2B     2014            46 Retrap      greti       11       NA          NA       NA          NA       NA          NA
5 2B     2014            46 Retrap      marti        1       NA          NA       NA          NA       NA          NA
6 2C     2014            25 New Capture bluti        2        2          NA       NA          NA       NA          NA
> 

假設它們描述了Year中對某個Site的個人的觀察,無論是新觀察還是重新觀察Status ,所有個體的最大數量Maxcount_site ,然后將這些觀察分解為類別,例如blutinew是對 bluti 的所有觀察是New Capture等等。

我想將所有具有相同Site , Year的行合並為一行,使其看起來像這樣,同時刪除 cols Namestatus以及maxcount

Site Year Maxcount_site blutinew blutiretrap gretinew gretiretrap martinew martiretrap
1   2B 2014            46       17          14        3          11        0           1
2   2C 2014            25        2          13        2           5        0           3
3   FS 2014            19        7           9        1           2        0           0
4   HE 2014            10        5           4        1           0        0           0

這是一個簡單的group_by的工作並summarize

library(tidyverse)
df |> 
  group_by(Site, Year) |> 
  summarize(across(-c(Maxcount_site, Status, Name), sum, na.rm = TRUE)) |> 
  ungroup()

# A tibble: 4 × 9
  Site   Year maxcount blutinew blutiretrap gretinew gretiretrap martinew martiretrap
  <chr> <dbl>    <int>    <int>       <int>    <int>       <int>    <int>       <int>
1 2B     2014       46       17          14        3           0        0           0
2 2C     2014       25        2          13        2           0        0           0
3 FS     2014       19        7           9        1           0        0           0
4 HE     2014       10        5           4        1           0        0           0

使用來自base Raggregate

aggregate(.~ Site + Year, subset(df, 
   select= -c(Maxcount_site, Status, Name)), sum, na.rm = TRUE, na.action = NULL)
  Site Year maxcount blutinew blutiretrap gretinew gretiretrap martinew martiretrap
1   2B 2014       46       17          14        3           0        0           0
2   2C 2014       25        2          13        2           0        0           0
3   FS 2014       19        7           9        1           0        0           0
4   HE 2014       10        5           4        1           0        0           0

暫無
暫無

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

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