这个问题的答案取决于您对可能出现在“空白”字符串中的事物的偏执程度。这是一种相当谨慎的方法,它将匹配零长度的空白字符串""
以及由一个或多个[[: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