找回密码
 注册
查看: 9246|回复: 1

R语言 大小写敏感问题 以及大小写之间转换

[复制链接]
发表于 2011-4-28 16:38:00 | 显示全部楼层 |阅读模式
本帖最后由 genechip 于 2011-4-28 16:41 编辑

R本身是case sensitive 的,但是在实际情况中也有不想要这个特性的时候,比如
intersect(a,b)
那么,该如何使该函数case insentive呢?

tolower()
toupper()
casefold()
做相应变换






英文说明如下
Character Translation and CasefoldingDescriptionTranslate characters in character vectors, in particular from upper to lower case or vice versa.
Usagechartr(old, new, x)tolower(x)toupper(x)casefold(x, upper = FALSE)Arguments
xa character vector, or an object that can be coerced to character by [url=mkMSITStore:C:\PROGRA~1\R\R-28~1.1\library\base\chtml\base.chm::/character.html]as.character[/url].
olda character string specifying the characters to be translated.
newa character string specifying the translations.
upperlogical: translate to upper or lower case?.

Detailschartr 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.
tolower and toupper convert upper-case characters in a character vector to lower-case, or vice versa. Non-alphabetic characters are left unchanged.
casefold is a wrapper for tolower and toupper provided for compatibility with S-PLUS.
ValueA character vector of the same length and with the same attributes as x (after possible coercion).
Elements of the result will be have the encoding declared as that of the current locale (see [url=mkMSITStore:C:\PROGRA~1\R\R-28~1.1\library\base\chtml\base.chm::/Encoding.html]Encoding[/url] 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.
See Also for other substitutions in strings.
Examplesx <- "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 ) :.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"## 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"capwords(c("using AIC", "for MODEL selection"), strict=TRUE)## ->  [1] "Using Aic"  "For Model Selection"##                ^^^        ^^^^^##               '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)))
回复

使用道具 举报

发表于 2011-5-5 16:00:56 | 显示全部楼层
学习学习啊,
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|生物统计家园 网站价格

GMT+8, 2024-11-22 03:02 , Processed in 0.040008 second(s), 15 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表