chartr(base)
chartr()所属R语言包:base
Character Translation and Casefolding
字符翻译和Casefolding的
译者:生物统计家园网 机器人LoveR
描述----------Description----------
Translate characters in character vectors, in particular from upper to lower case or vice versa.
在字符向量转换的字符,特别是从上小写或反之亦然。
用法----------Usage----------
chartr(old, new, x)
tolower(x)
toupper(x)
casefold(x, upper = FALSE)
参数----------Arguments----------
参数:x
a character vector, or an object that can be coerced to character by as.character.
一个字符向量,或者一个可以强制as.character字符的对象。
参数:old
a character string specifying the characters to be translated. If a character vector of length 2 or more is supplied, the first element is used with a warning.
一个字符串指定的字符被翻译。如果字符长度为2个或更多的向量提供,第一个元素是使用一个警告。
参数:new
a character string specifying the translations. If a character vector of length 2 or more is supplied, the first element is used with a warning.
字符串指定的翻译。如果字符长度为2个或更多的向量提供,第一个元素是使用一个警告。
参数:upper
logical: translate to upper or lower case?.
逻辑:转换为大写或小写。
Details
详情----------Details----------
chartr translates each character in x that is specified in old to the corresponding character specified in new. Ranges are supported in the specifications, but character classes and repeated characters are not. If old contains more characters than new, an error is signaled; if it contains fewer characters, the extra characters at the end of new are ignored.
chartr每个字符转换xoldnew指定相应的字符指定。支持范围的规格,但不是字符类和重复的字符。如果old包含比新的更多的字符,一个错误的信号,如果它包含更少的字符,多余的字符在年底new被忽略。
tolower and toupper convert upper-case characters in a character vector to lower-case, or vice versa. Non-alphabetic characters are left unchanged.
tolower和toupper在字符向量转换大写字符,小写,反之亦然。非字母字符保持不变。
casefold is a wrapper for tolower and toupper provided for compatibility with S-PLUS.
casefold是一个包装tolower和toupper,S-PLUS的兼容性提供。
值----------Value----------
A character vector of the same length and with the same attributes as x (after possible coercion).
一个相同的长度和x(后可能强制)相同的属性的特征向量。
Elements of the result will be have the encoding declared as that of the current locale (see Encoding if the corresponding input had a declared encoding and the current locale is either Latin-1 or UTF-8. The result will be in the current locale's encoding unless the corresponding input was in UTF-8, when it will be in UTF-8 when the system has Unicode wide characters.
元素的结果将有编码声明作为当前的区域设置(见Encoding如果相应的输入有一个声明的编码和当前的语言环境是为Latin-1或UTF-8。结果将在当前语言环境的,除非相应的输入编码是UTF-8时,它会以UTF-8时,该系统具有Unicode宽字符。
参见----------See Also----------
sub and gsub for other substitutions in strings.
sub和gsub在其他字符串替换。
举例----------Examples----------
x <- "MiXeD cAsE 123"
chartr("iXs", "why", x)
chartr("a-cX", "D-Fw", x)
tolower(x)
toupper(x)
## "Mixed Case" Capitalizing - toupper( every first letter of a word ) :[#“混合大小写”资本 - TOUPPER(每个单词的第一个字母):]
.simpleCap <- function(x) {
s <- strsplit(x, " ")[[1]]
paste(toupper(substring(s, 1,1)), substring(s, 2),
sep="", collapse=" ")
}
.simpleCap("the quick red fox jumps over the lazy brown dog")
## -> [1] "The Quick Red Fox Jumps Over The Lazy Brown Dog"[# - > [1]“快速红狐狸跳了懒惰的黄狗”]
## and the better, more sophisticated version:[#和更好,更先进的版本:]
capwords <- function(s, strict = FALSE) {
cap <- function(s) paste(toupper(substring(s,1,1)),
{s <- substring(s,2); if(strict) tolower(s) else s},
sep = "", collapse = " " )
sapply(strsplit(s, split = " "), cap, USE.NAMES = !is.null(names(s)))
}
capwords(c("using AIC for model selection"))
## -> [1] "Using AIC For Model Selection"[# - > [1]“AIC的模型选择使用”]
capwords(c("using AIC", "for MODEL selection"), strict=TRUE)
## -> [1] "Using Aic" "For Model Selection"[# - > [1]“”模型选择采用AIC“]
## ^^^ ^^^^^[#^ ^ ^ ^ ^ ^ ^ ^]
## 'bad' 'good'[#坏的好]
## -- Very simple insecure crypto --[ - 很简单,不安全的加密 - ]
rot <- function(ch, k = 13) {
p0 <- function(...) paste(c(...), collapse="")
A <- c(letters, LETTERS, " '")
I <- seq_len(k); chartr(p0(A), p0(c(A[-I], A[I])), ch)
}
pw <- "my secret pass phrase"
(crypw <- rot(pw, 13)) #-> you can send this off[ - >您可以发送这一关]
## now ``decrypt'' :[#现在“解密”:]
rot(crypw, 54 - 13)# -> the original:[ - >原:]
stopifnot(identical(pw, rot(crypw, 54 - 13)))
转载请注明:出自 生物统计家园网(http://www.biostatistic.net)。
注:
注1:为了方便大家学习,本文档为生物统计家园网机器人LoveR翻译而成,仅供个人R语言学习参考使用,生物统计家园保留版权。
注2:由于是机器人自动翻译,难免有不准确之处,使用时仔细对照中、英文内容进行反复理解,可以帮助R语言的学习。
注3:如遇到不准确之处,请在本贴的后面进行回帖,我们会逐渐进行修订。
|