A<-c("I","like","R")
A
#字符创长度
nchar(A) #1 4 1
#向量长度
length(A) #3
#paste 拼接字符串,可以将不是字符型的变量转换字符型再拼接
paste("This is" ,date()) #"This is Wed Oct 30 17:25:21 2024"
paste("x",1:10,sep = "_") # "x_1" "x_2" "x_3" "x_4" "x_5" "x_6" "x_7" "x_8" "x_9" "x_10"
paste("x",1:10,sep = "") #"x1" "x2" "x3" "x4" "x5" "x6" "x7" "x8" "x9" "x10"
paste("x",1:10,sep = "",collapse=";") # "x1;x2;x3;x4;x5;x6;x7;x8;x9;x10"
#strsplit paste的逆函数
s<-c("I like R","Me too")
strsplit(s,split = "") #"I" " " "l" "i" "k" "e" " " "R"
strsplit(s,split = " ") #"I" "like" "R"
#substr,提取或替换字符串中的字符
#提取
s1<-"abcdefg"
substr(s1,1,3) #"abc"
#赋值
substr(s1,1,3)<-"uvw" #"abc"
s1 #"uvwdefg"
#替换匹配到的字母,不改变原变量
s2<-sub("u","U",s1)
s2
#替换匹配到的所有字母,不改变原变量
gub("u","U",s1)
#转换为大写
toupper("abcd")
#转换为小写
tolower("ABCD")
#apply
#求平均值
#lapply()
#sapply()
#mapply()