How to remove rows with NAs only if they are present in more than certain percentage of columns?

和自甴很熟 提交于 2019-11-28 03:03:47

问题


I want to use na.omit (data) for the following example dataset, but on a condition so as to remove rows with NAs only when they are present in lets say "more than 30%" of the columns.

data:

        C1     C2     C3     C4     C5
Gene1   0.07   NA     0.05   0.07   0.07
Gene2   0.2    0.18   0.16   0.15   0.15
Gene3   NA     0.93   0.9    NA     0.92
Gene4   0.32   0.05   0.12   0.13   0.05
Gene5   0.44   0.53   0.46   0.03   0.47
Gene6   NA     0.34   NA     0.8    NA
Gene7   0.49   0.55   0.67   0.49   0.89
Gene8   0.25   NA     0.49   NA     NA
Gene9   0.1    0.1    0.05   NA     0.09

So the resulting file should be as follows:

        C1     C2     C3     C4     C5
Gene1   0.07   NA     0.05   0.07   0.07
Gene2   0.2    0.18   0.16   0.15   0.15
Gene4   0.32   0.05   0.12   0.13   0.05
Gene5   0.44   0.53   0.46   0.03   0.47
Gene7   0.49   0.55   0.67   0.49   0.89
Gene9   0.1    0.1    0.05   NA     0.09

Thanks for the help!


回答1:


You can subset based on the row sums of NA values:

test[!rowSums(is.na(test)) > ncol(test)*.3,]

        C1   C2   C3   C4   C5
Gene1 0.07   NA 0.05 0.07 0.07
Gene2 0.20 0.18 0.16 0.15 0.15
Gene4 0.32 0.05 0.12 0.13 0.05
Gene5 0.44 0.53 0.46 0.03 0.47
Gene7 0.49 0.55 0.67 0.49 0.89
Gene9 0.10 0.10 0.05   NA 0.09



回答2:


Here is another version with Reduce

df1[!Reduce(`+`, lapply(df1, is.na)) > ncol(df1)*0.3,]
#       C1   C2   C3   C4   C5
#Gene1 0.07   NA 0.05 0.07 0.07
#Gene2 0.20 0.18 0.16 0.15 0.15
#Gene4 0.32 0.05 0.12 0.13 0.05
#Gene5 0.44 0.53 0.46 0.03 0.47
#Gene7 0.49 0.55 0.67 0.49 0.89
#Gene9 0.10 0.10 0.05   NA 0.09



回答3:


And here another option using apply

dat[apply(dat,1,function(x){sum(is.na(x))/dim(dat)[2]})<0.3,]

#C1   C2   C3   C4   C5
#Gene1 0.07   NA 0.05 0.07 0.07 
#Gene2 0.20 0.18 0.16 0.15 0.15
#Gene4 0.32 0.05 0.12 0.13 0.05
#Gene5 0.44 0.53 0.46 0.03 0.47
#Gene7 0.49 0.55 0.67 0.49 0.89
#Gene9 0.10 0.10 0.05   NA 0.09


来源:https://stackoverflow.com/questions/37879467/how-to-remove-rows-with-nas-only-if-they-are-present-in-more-than-certain-percen

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!