字符合并
paste(“字符串1”,“字符串2”,sep=“分隔符”)
paste("abc", "bc", sep="-")

大小写转换
注意字符用单引号‘...’而不是“...”
全部转为大写
toupper('aB cd')
全部转为小写
tolower(c('aB', 'cd'))
应用,转换完方便做进一步处理做判断
toupper('jan')=='JAN'

取字符串的长度
nchar("abcccc")

取字符串子集
substr("abcdef", 2, 3)
substr("abcdef", 2, 5)

第3个开始直接取到最后
substring("abcdef", 3)

替换字符
gsub("被替换的字符",“替换字符”,替换的数据集)
gsub("abc", "", c("abc", "abcc", "abcbc"))
gsub("abc", "123", c("abc", "abcc", "abcbc"))

字符分离
strsplit("需要划分的字符串",“以哪里划分”,fixed=T/F)
当fixed=F是启用正则表达式。实例中2.2中的.可以匹配任何东西
fixed=T时。“.”就是“.”
strsplit("a;b;c", ";", fixed = T)
strsplit("a222b2.2c", "2.2", fixed = F)
strsplit("a222b2.2c", "2.2", fixed = T)

对字符向量排序
返回升序结果
v <- c("a", "d", "z", "b")
sort.result <- sort(v)
sort.result

返回降序结果加参数decreasing=TRUE
revsort.result <- sort(v, decreasing = TRUE)
print(revsort.result)

字符排列后,返回按顺序返回是原来向量中的第几个数
order函数
v <- c("a", "d", "z", "b")
order.result <- order(v)
print(order.result)
revordert.result <- order(v, decreasing = TRUE)
print(revordert.result)

文本排列实例演示
test=read.table("test2.txt",sep="\t",header=T,row.names = 1)
test
sort(test$height)
order(test$height)
test[order(test$height),]

网友评论