mro*_*opa 345 whitespace r trim removing-whitespace r-faq
我在data.frame中遇到了前导和尾随空格的麻烦.例如,我想看看在特定row的data.frame基础上有一定的条件:
> myDummy[myDummy$country == c("Austria"),c(1,2,3:7,19)]
[1] codeHelper country dummyLI dummyLMI dummyUMI
[6] dummyHInonOECD dummyHIOECD dummyOECD
<0 rows> (or 0-length row.names)
Run Code Online (Sandbox Code Playgroud)
我想知道为什么我没有得到预期的产量,因为奥地利显然存在于我的国家data.frame.在查看我的代码历史并试图弄清楚出了什么问题后,我尝试了:
> myDummy[myDummy$country == c("Austria "),c(1,2,3:7,19)]
codeHelper country dummyLI dummyLMI dummyUMI dummyHInonOECD dummyHIOECD
18 AUT Austria 0 0 0 0 1
dummyOECD
18 1
Run Code Online (Sandbox Code Playgroud)
我在命令中改变的是奥地利之后的另一个空格.
显然会出现更烦人的问题.例如,当我想根据国家/地区列合并两个帧时.一个data.frame用于"Austria "另一个帧"Austria".匹配不起作用.
到目前为止,我曾经写过一个Perl删除空格的简单脚本,但如果我可以在R里面以某种方式做到这一点会很好.
wli*_*erg 506
从R 3.2.0开始,引入了一个用于删除前导/尾随空格的新函数:
trimws()
Run Code Online (Sandbox Code Playgroud)
请参阅:http://stat.ethz.ch/R-manual/R-patched/library/base/html/trimws.html
f3l*_*lix 446
可能最好的方法是在读取数据文件时处理尾随空格.如果您使用read.csv或read.table您可以设置参数strip.white=TRUE.
如果您想在之后清理字符串,可以使用以下函数之一:
# returns string w/o leading whitespace
trim.leading <- function (x) sub("^\\s+", "", x)
# returns string w/o trailing whitespace
trim.trailing <- function (x) sub("\\s+$", "", x)
# returns string w/o leading or trailing whitespace
trim <- function (x) gsub("^\\s+|\\s+$", "", x)
Run Code Online (Sandbox Code Playgroud)
要使用以下功能之一myDummy$country:
myDummy$country <- trim(myDummy$country)
Run Code Online (Sandbox Code Playgroud)
要"显示"您可以使用的空白:
paste(myDummy$country)
Run Code Online (Sandbox Code Playgroud)
它将显示由引号(")包围的字符串,使空格更容易被发现.
use*_*rJT 87
要操作空格,请在stringr包中使用str_trim().该软件包的手册日期为2013年2月15日,并且在CRAN中.该函数还可以处理字符串向量.
install.packages("stringr", dependencies=TRUE)
require(stringr)
example(str_trim)
d4$clean2<-str_trim(d4$V2)
Run Code Online (Sandbox Code Playgroud)
(学分归于评论者:R.Cotton)
Ber*_*ler 23
一个删除前导和尾随空格的简单函数:
trim <- function( x ) {
gsub("(^[[:space:]]+|[[:space:]]+$)", "", x)
}
Run Code Online (Sandbox Code Playgroud)
用法:
> text = " foo bar baz 3 "
> trim(text)
[1] "foo bar baz 3"
Run Code Online (Sandbox Code Playgroud)
Mar*_*rek 11
ad1)要查看空格,您可以print.data.frame使用修改后的参数直接调用:
print(head(iris), quote=TRUE)
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# 1 "5.1" "3.5" "1.4" "0.2" "setosa"
# 2 "4.9" "3.0" "1.4" "0.2" "setosa"
# 3 "4.7" "3.2" "1.3" "0.2" "setosa"
# 4 "4.6" "3.1" "1.5" "0.2" "setosa"
# 5 "5.0" "3.6" "1.4" "0.2" "setosa"
# 6 "5.4" "3.9" "1.7" "0.4" "setosa"
Run Code Online (Sandbox Code Playgroud)
另请参阅?print.data.frame其他选项.
使用grep或grepl查找带有空格和子的观察结果以消除它们.
names<-c("Ganga Din\t","Shyam Lal","Bulbul ")
grep("[[:space:]]+$",names)
[1] 1 3
grepl("[[:space:]]+$",names)
[1] TRUE FALSE TRUE
sub("[[:space:]]+$","",names)
[1] "Ganga Din" "Shyam Lal" "Bulbul"
Run Code Online (Sandbox Code Playgroud)
如果输入之间有多个空格,则会出现另一个相关问题:
> a <- " a string with lots of starting, inter mediate and trailing whitespace "
Run Code Online (Sandbox Code Playgroud)
然后,您可以使用split参数的正则表达式轻松地将此字符串拆分为“真实”标记:
> strsplit(a, split=" +")
[[1]]
[1] "" "a" "string" "with" "lots"
[6] "of" "starting," "inter" "mediate" "and"
[11] "trailing" "whitespace"
Run Code Online (Sandbox Code Playgroud)
注意,如果在(非空)字符串的开头有匹配,则输出的第一个元素是'""',但如果在字符串的末尾有匹配,则输出与删除了比赛。
小智 5
我更愿意将答案作为评论添加到user56,但却无法写作独立答案.删除前导和尾随空白也可以通过gdata包中的trim()函数来实现:
require(gdata)
example(trim)
Run Code Online (Sandbox Code Playgroud)
用法示例:
> trim(" Remove leading and trailing blanks ")
[1] "Remove leading and trailing blanks"
Run Code Online (Sandbox Code Playgroud)
另一种选择是使用包中的stri_trim函数stringi,默认为删除前导和尾随空格:
> x <- c(" leading space","trailing space ")
> stri_trim(x)
[1] "leading space" "trailing space"
Run Code Online (Sandbox Code Playgroud)
要仅删除前导空格,请使用stri_trim_left.要仅删除尾随空格,请使用stri_trim_right.如果要删除其他前导或尾随字符,则必须指定pattern =.
另请参阅?stri_trim更多信息.
小智 5
我创建了一个trim.strings ()函数来修剪前导和/或尾随空格:
# Arguments: x - character vector
# side - side(s) on which to remove whitespace
# default : "both"
# possible values: c("both", "leading", "trailing")
trim.strings <- function(x, side = "both") {
if (is.na(match(side, c("both", "leading", "trailing")))) {
side <- "both"
}
if (side == "leading") {
sub("^\\s+", "", x)
} else {
if (side == "trailing") {
sub("\\s+$", "", x)
} else gsub("^\\s+|\\s+$", "", x)
}
}
Run Code Online (Sandbox Code Playgroud)
为了说明,
a <- c(" ABC123 456 ", " ABC123DEF ")
# returns string without leading and trailing whitespace
trim.strings(a)
# [1] "ABC123 456" "ABC123DEF"
# returns string without leading whitespace
trim.strings(a, side = "leading")
# [1] "ABC123 456 " "ABC123DEF "
# returns string without trailing whitespace
trim.strings(a, side = "trailing")
# [1] " ABC123 456" " ABC123DEF"
Run Code Online (Sandbox Code Playgroud)