nchar ("Hello World")#统计字符串的长度,这里空格也算作字符串
#nchar统计字符串长度,包括空格
month.name
nchar(month.name)
length(month.name)
#length输出向量中元素的个数,nchar输出向量中每个元素(字符串)的长度
paste("Everybody","loves","stats")
paste("Everybody","loves","stats",sep=‘-‘)
#paste连接向量中各字符串,sep可设置连接方式,默认使用空格分割
paste(1:12)
paste0(1:12)
class(paste(1:12))
#若paste和paste0的对象为单个向量,作用与as.character相同,将对象改为字符串
names <- c("Moe","Larry","Curly")
paste(names,"love stats")
substr(month.name,1,3)
#substr用于提取字符串,start决定从字符串第几位开始提取,stop决定在第几位结束
Mon <- substr(month.name,1,3)
toupper(Mon)#转换为大写
tolower(Mon)#小写
#toupper将对象改为大写,tolower改为小写
gsub("^(\\w)", "\\U\\1",tolower(MON))#这里的g是global
gsub("^(\\w)", "\\U\\1",tolower(Mon),perl = TRUE)
gsub("^(\\w)", "\\L\\1",toupper(Mon),perl = TRUE)
#如何只将首字母改为大写
x <- c("b","A+","AC")
grep ("A+",x,fixed=TRUE)#fixed=T使用精确匹配,返回元素所在的位置
grep ("A+",x,fixed=FALSE)#这里+表示可以匹配1到正无穷个A
match("AC",x)
#grep和match均可用于字符串匹配
path <- "/usr/local/bin/R"
strsplit(path,"/")
strsplit(c(path,path),"/")
#strsplit用于字符串的分割,strsplit(待分割字符串,“用于分割的标志”),分割后以list形式存储
face <- 1:13
suit <- c("spades","clubs","hearts","diamonds")
outer(suit,face,FUN=paste)#outer默认输出两数组的笛卡儿积,但使用FUN可修改
outer(suit,face,FUN=paste,sep="-")
#时间数据单独拿出来看作一种数据类型,称为时间序列
#时间分析分为对时间的描述和利用结果进行预测
sunspots#1749到1983年每月的太阳黑子数
presidents
airquality$Solar.R
airmiles
Sys.Date()#查看系统当前的时间
#Sys.date:当前系统时间
a="2017-01-01"
as.Date(a)#将元素变为日期类
b=as.Date(a,format="%Y-%m-%d")#使用format格式化日期
class(b)
#as.date将字符串转化为时间,但要规定每部分的意义
?strftime
#strftime可查看更多格式化参数
seq(as.Date("2017-01-01"),as.Date("2017-07-05"),by=5)
#seq可用于生成连续的时间点
sales <- round(runif(48,min=50,max=100))
#runif可生成随机数,round将其取整
ts(sales,start = c(2010,5),end = c(2014,4),frequency = 1)
ts(sales,start = c(2010,5),end = c(2014,4),frequency = 4)
ts(sales,start = c(2010,5),end = c(2014,4),frequency = 12)
#ts生成时间序列
#1指按年分;4指按季度分;12指按月分
x <- (1,2,3)#使用了中文的","
x < -c(1,2,3)#" <- "间有空格,这里变成了逻辑比较
#alt+‘-’可生成‘ <- ’!!!!==
x <- matrix(c(1:20,seq(1,12,3),4,4)#x <- matrix(c(1:20,seq(1,12,3)),4,4)
x <- c(one,two,three,4,5)#字符串加“”
install.packages (gclus)#未安装的包要加“”
x <- c(1,2,3,)#最后,去掉
state.x77[1]#state.x77[1,]或state.x77[,1]
plot(women$weight,women$height,pch=2,)#去,
a=5
b=8
a==b
a=b
setwd("c:\Users\Default\")#应该是/
x <- 1+2*3^4>5|| 15%%3>2
原文:https://www.cnblogs.com/kwq717/p/14633240.html