排除R中的空白和NA [英] Exclude Blank and NA in R

查看:279
本文介绍了排除R中的空白和NA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
R-删除data.frame中具有NA的行

Possible Duplicate:
R - remove rows with NAs in data.frame

我有一个名为sub.new的数据框,其中有多列.并且我试图排除任何包含NAblank space""的单元格.
我尝试使用subset(),但它的目标条件是特定的列.无论如何,有没有要扫描整个数据帧并创建一个子集,该子集的任何单元格都不是NAblank space?

I have a dataframe named sub.new with multiple columns in it. And I'm trying to exclude any cell containing NA or a blank space "".
I tried to use subset(), but it's targeting specific column conditional. Is there anyway to scan through the whole dataframe and create a subset that no cell is either NA or blank space ?

在下面的示例中,应仅保留第一行:

In the example below, only the first line should be kept:

# ID               SNP             ILMN_Strand   Customer_Strand
ID1234              [A/G]          TOP           BOT
Non-Specific        NSB (Bgnd)     Green
Non-Polymorphic     NP (A)         Red
Non-Polymorphic     NP (T)         Purple
Non-Polymorphic     NP (C)         Green
Non-Polymorphic     NP (G)         Blue
Restoration         Restore        Green

有什么建议吗?谢谢

推荐答案

一个好主意是在进行任何进一步分析之前,将所有"(空白单元格)设置为NA.

A good idea is to set all of the "" (blank cells) to NA before any further analysis.

如果您要从文件中读取输入内容,则最好将所有"都强制转换为NA:

If you are reading your input from a file, it is a good choice to cast all "" to NAs:

foo <- read.table(file="Your_file.txt", na.strings=c("", "NA"), sep="\t") # if your file is tab delimited

如果已经加载了表,则可以执行以下操作:

If you have already your table loaded, you can act as follows:

foo[foo==""] <- NA

然后仅保留没有NA的行,您可以只使用na.omit():

Then to keep only rows with no NA you may just use na.omit():

foo <- na.omit(foo)

或保留不包含NA的列:

Or to keep columns with no NA:

foo <- foo[, colSums(is.na(foo)) == 0] 

这篇关于排除R中的空白和NA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆