【问题标题】:How to consolidate duplicates如何合并重复项
【发布时间】:2021-11-20 02:33:42
【问题描述】:

我在一个数据集中有几个重复的条目。我想组合然后将结束列添加在一起。见下文。

我有什么:

Main | FLOW22016| FLOW2| Forest Lakes| 2016| 2016-10-03| 0| Creek chub| Semotilus atromaculatus| 1|

Main | FLOW22016| FLOW2| Forest Lakes| 2016| 2016-10-03| 0| Creek chub| Semotilus atromaculatus| 1|

我想要什么

Main | FLOW22016| FLOW2| Forest Lakes| 2016| 2016-10-03| 0| Creek chub| Semotilus atromaculatus | 2|

这可能吗?我有很多这样的数据点,我想合并。

【问题讨论】:

  • 请查看my answer below。下次请提供reproducible example,谢谢!
  • 请检查我的 :) @jay.sf
  • 请提供足够的代码,以便其他人更好地理解或重现问题。

标签: r dataframe duplicates


【解决方案1】:

试试,

library(dplyr)

df %>%
  group_by(across(c( - lastcolumn))) %>%
  summarise(lastcolumn = n())

其中lastcolumn 是为 1 的列,需要变为 2。

【讨论】:

    【解决方案2】:

    首先,将重复项放入数量递增的 bin 中,对它们进行计数并unique 数据框。

    R>4.1 中你可以这样做:

    dat <- transform(dat, bin=cumsum(!duplicated(dat[-(ncol(dat))]))) |>
      transform(V13=ave(V13, bin, FUN=sum)) |>
      unique()
    dat
    #     V1        V2    V3     V4    V5   V6         V7 V8    V9  V10          V11           V12 V13 bin
    # 1 Main FLOW22016 FLOW2 Forest Lakes 2016 2016-10-03  0 Creek chub    Semotilus atromaculatus   3   1
    # 4 Main FLOW22016 FLOW2   Nile river 2016 2016-10-03  0 Creek chub Chelaethiops         bibie   2   2
    

    数据

    dat <- read.table(header=TRUE, text='
        V1        V2    V3     V4    V5   V6         V7 V8    V9  V10       V11           V12 V13
     Main FLOW22016 FLOW2 Forest Lakes 2016 2016-10-03  0 Creek chub Semotilus atromaculatus   1           
     Main FLOW22016 FLOW2 Forest Lakes 2016 2016-10-03  0 Creek chub Semotilus atromaculatus   1           
     Main FLOW22016 FLOW2 Forest Lakes 2016 2016-10-03  0 Creek chub Semotilus atromaculatus   1           
     Main FLOW22016 FLOW2 Nile river 2016 2016-10-03  0 Creek chub Chelaethiops bibie   1           
     Main FLOW22016 FLOW2 Nile river 2016 2016-10-03  0 Creek chub Chelaethiops bibie   1           
               ')
    

    【讨论】:

      【解决方案3】:

      长格式通用 Base R 以保持唯一的非数字向量的值并对数字向量求和:

      # Function to group data by key: .grouping_func => function
      .grouping_func <- function(vec){
        # Calculate the run length encoding: r_l_e => rle
        r_l_e <- rle(vec)
        # Expand it out into the rle_id: rle_id => integer vector
        rle_id <- rep(
          seq_along(r_l_e$values), 
          times = r_l_e$lengths
        )
        # Explicitly define the return object rle_id => GlobalEnv
        return(rle_id)
      }
      
      # Function to resolve numeric vectors in a data.frame
      # resolve_num_vecs => function
      resolve_num_vecs <- function(df){
        # Resolve which vectors are numeric: 
        # num_cols => logical vector
        num_cols <- setNames(
          vapply(
            df, 
            is.numeric,
            logical(1)
          ),
          colnames(df)
        )
        # logical vector => Env
        return(num_cols)
      }
      
      # Function to combine list of data.frames into df: 
      # df_list_2_df => function
      df_list_2_df <- function(df_list, cmb_func = c(rbind, cbind)){
        # Resolve the desired combination function: 
        # cmb_func_resolved => character scalar
        cmb_func_resolved <- match.fun(cmb_func)
        # Combine list of data.frames into a data.frame 
        # using a given combination function: res => data.frame
        res <- data.frame(
          do.call(
            cmb_func_resolved,
            df_list
          ),
          row.names = NULL
        )
        # Explicitly define the returned object: 
        # data.frame => Env
        return(res)
      }
      
      # Function to bucket the data.frame 
      # bucket_df => function
      bucket_df <- function(df, logical_vector){
        # Apply the grouping function: res => character vector
        res <- .grouping_func(
          apply(
            df[,logical_vector],
            1,
            toString
          )
        )
        # Explicitly define returned object: 
        # character vector => Env
        return(res)
      }
      
      # Function to aggregate data.frame in specified manner:
      # agg_func => function
      agg_func <- function(df, logical_vector){
        # Column bind unique non-numeric columns with 
        # the sum of numeric columns into a data.frame:
        # res => data.frame
        res <- cbind(
          unique(df[,!logical_vector]),
          setNames(
            data.frame(
              t(
                colSums(df[,logical_vector])
              )
            ),
            names(logical_vector)[logical_vector]
          )
        )[,names(df)]
        # Explicitly define returned object: 
        # data.frame => Env
        return(res)
      }
      
      # Coerce the v6 vector to be of Date type:
      # v6 => Date vector
      dat$V6 <- as.Date(paste0(dat$V6, "-12-31"), "%Y-%m-%d")
      
      # Resolve which vectors are numeric: 
      # num_cols => logical vector
      num_cols <- resolve_num_vecs(dat)
      
      # Split-apply-combine by group using the 
      # defined aggregate function: res => data.frame
      res <- df_list_2_df( 
        lapply(
          split(
            dat, 
            bucket_df(
              dat, 
              !num_cols
            )
          ),
          function(x){
            agg_func(x, num_cols)
          }
        ),
        rbind
      )
      

      【讨论】:

        【解决方案4】:

        你的意思是:

        library(dplyr)
        df %>%
          group_by(lastcolumn) %>%
          mutate(lastcolumn = sum(lastcolumn))
        

        【讨论】:

          猜你喜欢
          • 2022-01-05
          • 1970-01-01
          • 2021-09-17
          • 1970-01-01
          • 2011-01-09
          • 2017-06-05
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多