代码之家  ›  专栏  ›  技术社区  ›  Andrew Brēza

在保持id变量的同时,告诉哪些行是不同的

  •  -2
  • Andrew Brēza  · 技术社区  · 7 年前

    我有一个藏书台,有1000多列和数十万行。我想去掉重复的值,同时保留每行的唯一ID值。这里是我尝试使用MTCars的简化版本。

    library(tidyverse)
    
    mtcars %>% 
      as_tibble() %>% 
      rownames_to_column() %>% 
      distinct(mpg:carb, .keep_all = TRUE)
    
    #Error in mutate_impl(.data, dots) : 
    #  Column `mpg:carb` must be length 32 (the number of rows) or one, not 18
    #In addition: Warning messages:
    #1: In mpg:carb : numerical expression has 32 elements: only the first used
    #2: In mpg:carb : numerical expression has 32 elements: only the first used
    

    在保持id变量的同时,如何删除非唯一行有什么想法吗?在mtcars示例中,id变量是 rownames . 列太多,我无法分别键入每一列。

    1 回复  |  直到 7 年前
        1
  •  1
  •   iod    7 年前
    df_filtered<-df[!duplicated(df[,-1]),]
    

    (这假设ID列是第一列)。 它所做的是为您提供数据帧的一个子集( df )它只包含除第一列以外的整行与前一行不重复的行。

    推荐文章