How to remove columns based on a specific row with 0?

时间:2019-05-31 11:50:26

标签: r dataframe filtering subset

I have the data frame that looks like below:

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

I wanted to remove the columns based on a row (Gene: A2M) with 0. So, if any of the samples with 0 for the row A2M should be removed. And the output should look like below:

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

2 个答案:

答案 0 :(得分:1)

We can use colSums, i.e.

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

答案 1 :(得分:0)

使用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))