我有一个721 x 26的数据帧。某些行的条目为空。它不是NULL 或NA,但只是空如下。如何删除那些包含这些条目的行?
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
答案 0 :(得分:5)
这个问题的答案取决于你想要对那些可能出现在“空白”字符串中的东西的偏执。这是一个相当小心的方法,它将匹配零长度空白字符串""
以及由一个或多个[[: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