【问题标题】:How to remove columns based on a specific row with 0?如何根据带有 0 的特定行删除列?
【发布时间】:2019-10-17 01:45:56
【问题描述】:

我的数据框如下所示:

Genes     Sample1    Sample2   Sample3   Sample4
A1BG         14         59        11        31
A2M           0        7708       7306       0
A2ML1        64         71       1317       3406
A4GALT      142          0       1195       700
AAAS        1821       1233        0        959

我想删除基于行(基因:A2M)为 0 的列。因此,如果应该删除行 A2M 的任何样本为 0。输出应如下所示:

Genes     Sample2   Sample3   
A1BG         59        11      
A2M         7708       7306     
A2ML1        71       1317     
A4GALT        0       1195       
AAAS        1233        0     

【问题讨论】:

    标签: r dataframe filtering subset


    【解决方案1】:

    我们可以使用colSums,即

    df[colSums(df[df$Genes == 'A2M',] == 0) == 0]
    
    #   Genes Sample2 Sample3
    #1   A1BG      59      11
    #2    A2M    7708    7306
    #3  A2ML1      71    1317
    #4 A4GALT       0    1195
    #5   AAAS    1233       0
    

    【讨论】:

      【解决方案2】:

      使用tidyverse的选项

      library(tidyverse)
      df1 %>% 
         filter(Genes == "A2M") %>% 
         select_if(~is.numeric(.x) & .x != 0) %>%
         names %>% 
         select(df1, .)
      #  Sample2 Sample3
      #1      59      11
      #2    7708    7306
      #3      71    1317
      #4       0    1195
      #5    1233       0
      

      数据

      df1 <- structure(list(Genes = c("A1BG", "A2M", "A2ML1", "A4GALT", "AAAS"
      ), Sample1 = c(14L, 0L, 64L, 142L, 1821L), Sample2 = c(59L, 7708L, 
      71L, 0L, 1233L), Sample3 = c(11L, 7306L, 1317L, 1195L, 0L), Sample4 = c(31L, 
      0L, 3406L, 700L, 959L)), class = "data.frame", row.names = c(NA, 
      -5L))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多