如何有条件地删除R中的行?

时间:2016-07-22 21:28:46

标签: r dataframe

我的日期框架如下:

@bread

我想删除 SNP CLST A1 A2 FRQ IMP POS CHR BVAL 1 rs2803291 Brahui C T 0.660000 0 1882185 1 878 2 rs2803291 Balochi C T 0.750000 0 1882185 1 878 3 rs2803291 Hazara C T 0.772727 0 1882185 1 878 4 rs2803291 Makrani C T 0.620000 0 1882185 1 878 5 rs2803291 Sindhi C T 0.770833 0 1882185 1 878 6 rs2803291 Pathan C T 0.681818 0 1882185 1 878 53 rs12060022 Brahui T C 0.0600000 1 3108186 1 982 54 rs12060022 Balochi T C 0.0416667 1 3108186 1 982 55 rs12060022 Hazara T C 0.0000000 1 3108186 1 982 56 rs12060022 Makrani T C 0.0200000 1 3108186 1 982 57 rs12060022 Sindhi T C 0.0625000 1 3108186 1 982 58 rs12060022 Pathan T C 1 1 3108186 1 982 105 rs870171 Brahui T G 0.2200000 0 3332664 1 976 106 rs870171 Balochi T G 0.3333330 0 3332664 1 976 107 rs870171 Hazara T G 1 0 3332664 1 976 108 rs870171 Makrani T G 1 0 3332664 1 976 109 rs870171 Sindhi T G 0.2083330 0 3332664 1 976 110 rs870171 Pathan T G 1 0 3332664 1 976 157 rs4282783 Brahui G T 1 1 4090545 1 992 158 rs4282783 Balochi G T 1 1 4090545 1 992 159 rs4282783 Hazara G T 1 1 4090545 1 992 160 rs4282783 Makrani G T 1 1 4090545 1 992 161 rs4282783 Sindhi G T 1 1 4090545 1 992 162 rs4282783 Pathan G T 1 1 4090545 1 992 列中给定SNP的每一行的值为1的所有行。例如,每个rs4282783在FRQ列中的值为1,因此我想删除所有这些行。但我不想删除第58行,例如FRQ中的值为1。有没有人有任何建议?

4 个答案:

答案 0 :(得分:3)

要删除SNP的所有值均等于1的FRQ,您可以尝试:

library(dplyr)
df %>%
  group_by(SNP) %>%
  filter(!all(FRQ == 1))

给出了:

#         SNP    CLST     A1     A2       FRQ   IMP     POS   CHR  BVAL
#       <fctr>  <fctr> <fctr> <fctr>     <dbl> <int>   <int> <int> <int>
#1   rs2803291  Brahui      C      T 0.6600000     0 1882185     1   878
#2   rs2803291 Balochi      C      T 0.7500000     0 1882185     1   878
#3   rs2803291  Hazara      C      T 0.7727270     0 1882185     1   878
#4   rs2803291 Makrani      C      T 0.6200000     0 1882185     1   878
#5   rs2803291  Sindhi      C      T 0.7708330     0 1882185     1   878
#6   rs2803291  Pathan      C      T 0.6818180     0 1882185     1   878
#7  rs12060022  Brahui      T      C 0.0600000     1 3108186     1   982
#8  rs12060022 Balochi      T      C 0.0416667     1 3108186     1   982
#9  rs12060022  Hazara      T      C 0.0000000     1 3108186     1   982
#10 rs12060022 Makrani      T      C 0.0200000     1 3108186     1   982
#11 rs12060022  Sindhi      T      C 0.0625000     1 3108186     1   982
#12 rs12060022  Pathan      T      C 1.0000000     1 3108186     1   982
#13   rs870171  Brahui      T      G 0.2200000     0 3332664     1   976
#14   rs870171 Balochi      T      G 0.3333330     0 3332664     1   976
#15   rs870171  Hazara      T      G 1.0000000     0 3332664     1   976
#16   rs870171 Makrani      T      G 1.0000000     0 3332664     1   976
#17   rs870171  Sindhi      T      G 0.2083330     0 3332664     1   976
#18   rs870171  Pathan      T      G 1.0000000     0 3332664     1   976

答案 1 :(得分:2)

这是使用子集和ave的基本R方法。 ave构造组级别(SNP级别)最大值,用于通过观察对数据进行子集化:

df[ave(df$FRQ, df$SNP, FUN=max) < 0.99999,]

        SNP    CLST A1 A2      FRQ IMP     POS CHR BVAL
1 rs2803291  Brahui  C  T 0.660000   0 1882185   1  878
2 rs2803291 Balochi  C  T 0.750000   0 1882185   1  878
3 rs2803291  Hazara  C  T 0.772727   0 1882185   1  878
4 rs2803291 Makrani  C  T 0.620000   0 1882185   1  878
5 rs2803291  Sindhi  C  T 0.770833   0 1882185   1  878
6 rs2803291  Pathan  C  T 0.681818   0 1882185   1  878

请注意,我使用0.99999而不是1来避免或减少数字不精确问题。

数据

df <- read.table(header=T, text="SNP  CLST A1 A2   FRQ IMP  POS CHR BVAL
1   rs2803291            Brahui  C  T  0.660000   0 1882185   1  878
2   rs2803291           Balochi  C  T  0.750000   0 1882185   1  878
3   rs2803291            Hazara  C  T  0.772727   0 1882185   1  878
4   rs2803291           Makrani  C  T  0.620000   0 1882185   1  878
5   rs2803291            Sindhi  C  T  0.770833   0 1882185   1  878
6   rs2803291            Pathan  C  T  0.681818   0 1882185   1  878
53  rs12060022           Brahui  T  C 0.0600000   1 3108186   1  982
54  rs12060022          Balochi  T  C 0.0416667   1 3108186   1  982
55  rs12060022           Hazara  T  C 0.0000000   1 3108186   1  982
56  rs12060022          Makrani  T  C 0.0200000   1 3108186   1  982
57  rs12060022           Sindhi  T  C 0.0625000   1 3108186   1  982
58  rs12060022           Pathan  T  C 1           1 3108186   1  982
105   rs870171           Brahui  T  G 0.2200000   0 3332664   1  976
106   rs870171          Balochi  T  G 0.3333330   0 3332664   1  976
107   rs870171           Hazara  T  G 1           0 3332664   1  976
108   rs870171          Makrani  T  G 1           0 3332664   1  976
109   rs870171           Sindhi  T  G 0.2083330   0 3332664   1  976
110   rs870171           Pathan  T  G 1           0 3332664   1  976
157  rs4282783           Brahui  G  T 1           1 4090545   1  992
158  rs4282783          Balochi  G  T 1           1 4090545   1  992
159  rs4282783           Hazara  G  T 1           1 4090545   1  992
160  rs4282783          Makrani  G  T 1           1 4090545   1  992
161  rs4282783           Sindhi  G  T 1           1 4090545   1  992
162  rs4282783           Pathan  G  T 1           1 4090545   1  992")

答案 2 :(得分:2)

@ imo的答案比较整洁,但正如我已经完成的那样,我会添加它。 在我看来,逻辑稍微清晰一些。

# which SNPs are always 1
# For each SNP value, take the rows with that SNP, and test if all FRQ values are 1
rmSNPs <- sapply(unique(dd$SNP), function(x) all(dd$FRQ[dd$SNP == x] == 1))

# new data is old data minus row where dd$SNP is not one of those found above
newdata <- dd[dd$SNP != unique(dd$SNP)[rmSNPs], ]

答案 3 :(得分:2)

这是一种基于data.table的方法。将'data.frame'转换为'data.table'(setDT(df)),按'SNP'分组,if not(!all'FRQ中的元素'是1,然后得到Data.table的子集。

library(data.table)
setDT(df)[, if(!(all(FRQ==1))) .SD , by = SNP]

或者一般的方法,如果我猜测OP意味着删除只有一个“FRQ”的所有SNP,那么我们可以使用uniqueN来查找unique元素的数量并用于if条件只保留那些超过1

的条件
setDT(df)[, if(uniqueN(FRQ) > 1) .SD , by = SNP]