我有一个721 x 26数据框。一些行具有空白的条目。它不是NULL 或NA,但只是空的如下。我如何删除那些有这些条目的行?R中有空白条目的行
1 Y N Y N 86.8
2 N N Y N 50.0
3 76.8
4 N N Y N 46.6
5 Y Y Y Y 30.0
我有一个721 x 26数据框。一些行具有空白的条目。它不是NULL 或NA,但只是空的如下。我如何删除那些有这些条目的行?R中有空白条目的行
1 Y N Y N 86.8
2 N N Y N 50.0
3 76.8
4 N N Y N 46.6
5 Y Y Y Y 30.0
这个问题的答案取决于你想要关于那些可能出现在'空白'字符串中的事情的偏执。这是一个非常小心的方法,它将匹配零长度的空字符串""
以及任意由一个或多个[[:space:]]
字符组成的字符串(即“制表符,换行符,垂直制表符,换页符,回车符,空格以及可能还有其他符合语言环境的字符串字符“,根据?regex
帮助页面)。
## An example data.frame containing all sorts of 'blank' strings
df <- data.frame(A = c("a", "", "\n", " ", " \t\t", "b"),
B = c("b", "b", "\t", " ", "\t\t\t", "d"),
C = 1:6)
## Test each element to see if is either zero-length or contains just
## space characters
pat <- "^[[:space:]]*$"
subdf <- df[-which(names(df) %in% "C")] # removes columns not involved in the test
matches <- data.frame(lapply(subdf, function(x) grepl(pat, x)))
## Subset df to remove rows fully composed of elements matching `pat`
df[!apply(matches, 1, all),]
# A B C
# 1 a b 1
# 2 b 2
# 6 b d 6
## OR, to remove rows with *any* blank entries
df[!apply(matches, 1, any),]
# A B C
# 1 a b 1
# 6 b d 6
您是否试过'==“”'匹配空格? – Gregor 2012-07-16 22:46:06
“空白”是什么意思?请使用'dput()' – Andrie 2012-07-16 22:46:17