R语言统计分析与应用-第二章-4
2.1.2 R运算符
这个部分相对简单些。R中的运算符分为算数运算符、比较算符、逻辑算符。
- 算数运算符
就是指加减乘除等等啦,见下表:
算数算符 | 含义 |
---|---|
+ | 加 |
- | 减 |
* | 乘 |
/ | 除 |
^ | 幂 |
%% | 模运算 |
%/% | 整数除法 |
- 比较算符
就是大于、小于、不等于等等啦,见下表:
比较算符 | 含义 |
---|---|
== | 等于 |
!= | 不等于 |
> | 大于 |
< | 小于 |
>= | 大于等于 |
<+ | 小于等于 |
- 逻辑算符
其实就是数学中的或与非啦,见下表:
逻辑算符 | 含义 |
---|---|
&& | 标量的“与”运算 |
& | 向量的“与”运算 |
! | 非 |
关于这个就不啰嗦了,或与非其实是高中数学内容了。 大家应该注意到上面说到了标量这个词,在R中,表面上没有标量的类型,但实际上它可以看做是含有一个元素的向量。下面的例子可以看出逻辑运算符对标量和向量的区别:
x <- c(TRUE,FALSE,TRUE)
y <- c(TRUE,TRUE,FALSE)
x & y
## [1] TRUE FALSE FALSE
x[1] && y[1]
## [1] TRUE
x && y
## [1] TRUE
if (x[1] && y[1])
print("both true")
## [1] "both true"
if (x & y)
print("both true")
## Warning in if (x & y) print("both true"): 条件的长度大于一,因此只能用其第
## 一元素
## [1] "both true"
可以看到最后一条语句报错了“the condition has length > 1 and only the first element will be used”。 这是因为if结构判断语句的取值,只能是一个逻辑值,而不是逻辑值的向量。
- 运算次序
这个~没啥好讲的~
2.2 R常用函数及其应用
嗯~~前面讲了很多R的基本结构,向量啦,矩阵啦什么的,现在再进一步,开始讲一下R的常用函数。
2.2.1 数学函数
#绝对值
abs(-1)
## [1] 1
#平方根
sqrt(36)
## [1] 6
25^(0.5)
## [1] 5
#不小于x的最小整数
ceiling(3.1415926)
## [1] 4
#不大于x的最大整数
floor(3.1415926)
## [1] 3
#向0的方向截取x中的整数部分
trunc(5.99)
## [1] 5
#将x舍入为指定位数的小数
round(3.1415926, digits = 2)
## [1] 3.14
#将x舍入为指定位数的有效数字
signif(3.1415926, digits = 2)
## [1] 3.1
#一些三角函数
cos(3.1415926)
## [1] -1
sin(3.1415926)
## [1] 5.358979e-08
tan(3.1415926)
## [1] -5.358979e-08
acos(3.1415926)
## Warning in acos(3.1415926): 产生了NaNs
## [1] NaN
asin(3.1415926)
## Warning in asin(3.1415926): 产生了NaNs
## [1] NaN
atan(3.1415926)
## [1] 1.262627
#双曲余弦
cosh(3.1415926)
## [1] 11.59195
#双曲正弦
sinh(3.1415926)
## [1] 11.54874
#反双曲余弦、正弦
acosh(3.1415926)
## [1] 1.811526
asinh(3.1415926)
## [1] 1.862296
#log(x, base=n),对x取以n为底的对数
log(8,2)
## [1] 3
#取自然对数
log(8)
## [1] 2.079442
#常用对数
log10(8)
## [1] 0.90309
log(8,10)
## [1] 0.90309
#指数函数
exp(2)
## [1] 7.389056
2.2.2 样本统计函数
#平均数
mean(c(1,2,3,4))
## [1] 2.5
#中位数
median(c(1,2,3,4))
## [1] 2.5
median(c(1,2,3,4,5))
## [1] 3
#标准差
sd(c(1,2,3,4))
## [1] 1.290994
#方差
var(c(1,2,3,4))
## [1] 1.666667
#绝对中位差
mad(c(1,2,3,4))
## [1] 1.4826
#quantile(x,probs),求分位数,x为待求分位数的数值型向量
#probs为一个由[0,1]之间的概率组成的数值向量
#求向量x的第25和75百分位数
quantile(c(1,2,3,4,5,6,7), c(.25,.75))
## 25% 75%
## 2.5 5.5
#求值域
range(c(1,2,3,4,5,6,7))
## [1] 1 7
#求和
sum(c(1,2,3,4,5,6,7))
## [1] 28
#最大值
max(c(1,2,3,4,5,6,7))
## [1] 7
#最小值
min(c(1,2,3,4,5,6,7))
## [1] 1
#scale(x,center=TRUE,scale=TRUE)
#将数据对象x按列进行中心化(center=TRUE)
#或标准化(center=TRUE,scale=TRUE)
#默认情况下,这个函数对矩阵或数据框的指定列进行均值为0
#标准差为1的标准化
mydata <- matrix(c(1,2,3,4,5,6), nrow = 2)
scale(mydata)
## [,1] [,2] [,3]
## [1,] -0.7071068 -0.7071068 -0.7071068
## [2,] 0.7071068 0.7071068 0.7071068
## attr(,"scaled:center")
## [1] 1.5 3.5 5.5
## attr(,"scaled:scale")
## [1] 0.7071068 0.7071068 0.7071068
#要对每一列进行任意均值和标准差的标准化
#可以采用下面的语句:mydata <- scale(mydata)*SD+M
#其中M是想要的均值,SD是想要的标准差
scale(mydata)*0.25+3
## [,1] [,2] [,3]
## [1,] 2.823223 2.823223 2.823223
## [2,] 3.176777 3.176777 3.176777
## attr(,"scaled:center")
## [1] 1.5 3.5 5.5
## attr(,"scaled:scale")
## [1] 0.7071068 0.7071068 0.7071068
2.2.4 字符处理函数
前面讲了很多函数,不过都是数值型的,现在接下来要讲的是有关字符处理的函数。 字符处理函数可以从文本型数据中抽取信息,或者为打印输出和生成报告重设文本的格式。
#返回字符串x的字符数量
x <- c("ab","cde","dsdesd")
length(x)
## [1] 3
nchar(x)
## [1] 2 3 6
nchar(x[3])
## [1] 6
#substr(x, start, stop)返回字符串x中指定位置范围的子字符串
x <- "abcdefghij"
substr(x, 2, 4)
## [1] "bcd"
substr(x, 2, 4) <- "22222"
x
## [1] "a222efghij"
#grep(pattern, x, ignore.case=FALSE, fixed=FALSE)
#在字符串x中搜索给定的子字符串pattern。
#若fixed=FALSE,则pattern为一个正则表达式
#若fixed=TRUE,则pattern为一个文本字符串,返回值为匹配的下标
grep("A",c("b","A","c"), fixed = TRUE)
## [1] 2
grep("A",c("b","A","c"), fixed = FALSE)
## [1] 2
#sub(pattern, replacement, x, ignore.case=FALSE, fixed=FALSE)在x中搜索pattern,并以文本replacement将其替换。
#若fixed=FALSE,则pattern为一个正则表达式
#若fixed=TRUE,则pattern为一个文本字符串
#下例中“\s”是一个查找空白的正则表达式
#使用“\\s”而不是“\”的原因在于后者是R中的转义字符
sub("\\s", ".", "Hello There")
## [1] "Hello.There"
#strsplit(x,split,fixed=FALSE)将在以split处分割字符向量x中
#将元素拆分为若干个子字符串,返回这些子字符串的列表
#若fixed=FALSE,则pattern为一个正则表达式
#若fixed=TRUE,则pattern为一个文本字符串
y <- strsplit("abc","")
y
## [[1]]
## [1] "a" "b" "c"
unlist(y)[2]
## [1] "b"
sapply(y,"[",2)
## [1] "b"
strsplit("6-16-2011",split = "-")
## [[1]]
## [1] "6" "16" "2011"
#paste(……, sep="")把若干个字符串拼接在一起,分隔符为sep
paste("x", 1:3, sep = "")
## [1] "x1" "x2" "x3"
paste("x", 1:3, sep = "M")
## [1] "xM1" "xM2" "xM3"
paste("Today is", date())
## [1] "Today is Fri Apr 19 20:30:41 2019"
paste("North", "Pole")
## [1] "North Pole"
paste("North", "Pole", sep = " ")
## [1] "North Pole"
paste("North", "Pole", sep = "")
## [1] "NorthPole"
paste("North", "Pole", sep = ".")
## [1] "North.Pole"
paste("North", "and", "Pole", "South", sep = "")
## [1] "NorthandPoleSouth"
#大写转换
toupper("abc")
## [1] "ABC"
#小写转换
tolower("ABC")
## [1] "abc"
#regexpr(pattern,x)在字符串x中寻找pattern
#返回与pattern匹配的第一个子字符串的起始字符位置
regexpr("uat", "Equator")
## [1] 3
## attr(,"match.length")
## [1] 3
## attr(,"index.type")
## [1] "chars"
## attr(,"useBytes")
## [1] TRUE
#gregexpr(pattern,x)与前一个功能一样
#不过它会寻找与pattern匹配的全部子字符串的起始位置
gregexpr("iss", "Missppiissist")
## [[1]]
## [1] 2 8
## attr(,"match.length")
## [1] 3 3
## attr(,"index.type")
## [1] "chars"
## attr(,"useBytes")
## [1] TRUE
gregexpr("uat", "Equator")
## [[1]]
## [1] 3
## attr(,"match.length")
## [1] 3
## attr(,"index.type")
## [1] "chars"
## attr(,"useBytes")
## [1] TRUE
2.2.5 其他实用函数
对象x的长度:
x <- c(1,2,3,4,5,6)
length(x)
## [1] 6
生成一个序列:
mysequ <- seq(1,20,3)
mysequ
## [1] 1 4 7 10 13 16 19
将x重复n次:
rep("ABC",3)
## [1] "ABC" "ABC" "ABC"
rep(1:3,3)
## [1] 1 2 3 1 2 3 1 2 3
将连续型变量x分割为有着n个水平的因子:
x <- c(1:1000)
cut(x, 5)
## [1] (0.001,201] (0.001,201] (0.001,201] (0.001,201] (0.001,201]
## [6] (0.001,201] (0.001,201] (0.001,201] (0.001,201] (0.001,201]
## [11] (0.001,201] (0.001,201] (0.001,201] (0.001,201] (0.001,201]
## [16] (0.001,201] (0.001,201] (0.001,201] (0.001,201] (0.001,201]
## [21] (0.001,201] (0.001,201] (0.001,201] (0.001,201] (0.001,201]
## [26] (0.001,201] (0.001,201] (0.001,201] (0.001,201] (0.001,201]
## [31] (0.001,201] (0.001,201] (0.001,201] (0.001,201] (0.001,201]
## [36] (0.001,201] (0.001,201] (0.001,201] (0.001,201] (0.001,201]
## [41] (0.001,201] (0.001,201] (0.001,201] (0.001,201] (0.001,201]
## [46] (0.001,201] (0.001,201] (0.001,201] (0.001,201] (0.001,201]
## [51] (0.001,201] (0.001,201] (0.001,201] (0.001,201] (0.001,201]
## [56] (0.001,201] (0.001,201] (0.001,201] (0.001,201] (0.001,201]
## [61] (0.001,201] (0.001,201] (0.001,201] (0.001,201] (0.001,201]
## [66] (0.001,201] (0.001,201] (0.001,201] (0.001,201] (0.001,201]
## [71] (0.001,201] (0.001,201] (0.001,201] (0.001,201] (0.001,201]
## [76] (0.001,201] (0.001,201] (0.001,201] (0.001,201] (0.001,201]
## [81] (0.001,201] (0.001,201] (0.001,201] (0.001,201] (0.001,201]
## [86] (0.001,201] (0.001,201] (0.001,201] (0.001,201] (0.001,201]
## [91] (0.001,201] (0.001,201] (0.001,201] (0.001,201] (0.001,201]
## [96] (0.001,201] (0.001,201] (0.001,201] (0.001,201] (0.001,201]
## [101] (0.001,201] (0.001,201] (0.001,201] (0.001,201] (0.001,201]
## [106] (0.001,201] (0.001,201] (0.001,201] (0.001,201] (0.001,201]
## [111] (0.001,201] (0.001,201] (0.001,201] (0.001,201] (0.001,201]
## [116] (0.001,201] (0.001,201] (0.001,201] (0.001,201] (0.001,201]
## [121] (0.001,201] (0.001,201] (0.001,201] (0.001,201] (0.001,201]
## [126] (0.001,201] (0.001,201] (0.001,201] (0.001,201] (0.001,201]
## [131] (0.001,201] (0.001,201] (0.001,201] (0.001,201] (0.001,201]
## [136] (0.001,201] (0.001,201] (0.001,201] (0.001,201] (0.001,201]
## [141] (0.001,201] (0.001,201] (0.001,201] (0.001,201] (0.001,201]
## [146] (0.001,201] (0.001,201] (0.001,201] (0.001,201] (0.001,201]
## [151] (0.001,201] (0.001,201] (0.001,201] (0.001,201] (0.001,201]
## [156] (0.001,201] (0.001,201] (0.001,201] (0.001,201] (0.001,201]
## [161] (0.001,201] (0.001,201] (0.001,201] (0.001,201] (0.001,201]
## [166] (0.001,201] (0.001,201] (0.001,201] (0.001,201] (0.001,201]
## [171] (0.001,201] (0.001,201] (0.001,201] (0.001,201] (0.001,201]
## [176] (0.001,201] (0.001,201] (0.001,201] (0.001,201] (0.001,201]
## [181] (0.001,201] (0.001,201] (0.001,201] (0.001,201] (0.001,201]
## [186] (0.001,201] (0.001,201] (0.001,201] (0.001,201] (0.001,201]
## [191] (0.001,201] (0.001,201] (0.001,201] (0.001,201] (0.001,201]
## [196] (0.001,201] (0.001,201] (0.001,201] (0.001,201] (0.001,201]
## [201] (201,401] (201,401] (201,401] (201,401] (201,401]
## [206] (201,401] (201,401] (201,401] (201,401] (201,401]
## [211] (201,401] (201,401] (201,401] (201,401] (201,401]
## [216] (201,401] (201,401] (201,401] (201,401] (201,401]
## [221] (201,401] (201,401] (201,401] (201,401] (201,401]
## [226] (201,401] (201,401] (201,401] (201,401] (201,401]
## [231] (201,401] (201,401] (201,401] (201,401] (201,401]
## [236] (201,401] (201,401] (201,401] (201,401] (201,401]
## [241] (201,401] (201,401] (201,401] (201,401] (201,401]
## [246] (201,401] (201,401] (201,401] (201,401] (201,401]
## [251] (201,401] (201,401] (201,401] (201,401] (201,401]
## [256] (201,401] (201,401] (201,401] (201,401] (201,401]
## [261] (201,401] (201,401] (201,401] (201,401] (201,401]
## [266] (201,401] (201,401] (201,401] (201,401] (201,401]
## [271] (201,401] (201,401] (201,401] (201,401] (201,401]
## [276] (201,401] (201,401] (201,401] (201,401] (201,401]
## [281] (201,401] (201,401] (201,401] (201,401] (201,401]
## [286] (201,401] (201,401] (201,401] (201,401] (201,401]
## [291] (201,401] (201,401] (201,401] (201,401] (201,401]
## [296] (201,401] (201,401] (201,401] (201,401] (201,401]
## [301] (201,401] (201,401] (201,401] (201,401] (201,401]
## [306] (201,401] (201,401] (201,401] (201,401] (201,401]
## [311] (201,401] (201,401] (201,401] (201,401] (201,401]
## [316] (201,401] (201,401] (201,401] (201,401] (201,401]
## [321] (201,401] (201,401] (201,401] (201,401] (201,401]
## [326] (201,401] (201,401] (201,401] (201,401] (201,401]
## [331] (201,401] (201,401] (201,401] (201,401] (201,401]
## [336] (201,401] (201,401] (201,401] (201,401] (201,401]
## [341] (201,401] (201,401] (201,401] (201,401] (201,401]
## [346] (201,401] (201,401] (201,401] (201,401] (201,401]
## [351] (201,401] (201,401] (201,401] (201,401] (201,401]
## [356] (201,401] (201,401] (201,401] (201,401] (201,401]
## [361] (201,401] (201,401] (201,401] (201,401] (201,401]
## [366] (201,401] (201,401] (201,401] (201,401] (201,401]
## [371] (201,401] (201,401] (201,401] (201,401] (201,401]
## [376] (201,401] (201,401] (201,401] (201,401] (201,401]
## [381] (201,401] (201,401] (201,401] (201,401] (201,401]
## [386] (201,401] (201,401] (201,401] (201,401] (201,401]
## [391] (201,401] (201,401] (201,401] (201,401] (201,401]
## [396] (201,401] (201,401] (201,401] (201,401] (201,401]
## [401] (401,600] (401,600] (401,600] (401,600] (401,600]
## [406] (401,600] (401,600] (401,600] (401,600] (401,600]
## [411] (401,600] (401,600] (401,600] (401,600] (401,600]
## [416] (401,600] (401,600] (401,600] (401,600] (401,600]
## [421] (401,600] (401,600] (401,600] (401,600] (401,600]
## [426] (401,600] (401,600] (401,600] (401,600] (401,600]
## [431] (401,600] (401,600] (401,600] (401,600] (401,600]
## [436] (401,600] (401,600] (401,600] (401,600] (401,600]
## [441] (401,600] (401,600] (401,600] (401,600] (401,600]
## [446] (401,600] (401,600] (401,600] (401,600] (401,600]
## [451] (401,600] (401,600] (401,600] (401,600] (401,600]
## [456] (401,600] (401,600] (401,600] (401,600] (401,600]
## [461] (401,600] (401,600] (401,600] (401,600] (401,600]
## [466] (401,600] (401,600] (401,600] (401,600] (401,600]
## [471] (401,600] (401,600] (401,600] (401,600] (401,600]
## [476] (401,600] (401,600] (401,600] (401,600] (401,600]
## [481] (401,600] (401,600] (401,600] (401,600] (401,600]
## [486] (401,600] (401,600] (401,600] (401,600] (401,600]
## [491] (401,600] (401,600] (401,600] (401,600] (401,600]
## [496] (401,600] (401,600] (401,600] (401,600] (401,600]
## [501] (401,600] (401,600] (401,600] (401,600] (401,600]
## [506] (401,600] (401,600] (401,600] (401,600] (401,600]
## [511] (401,600] (401,600] (401,600] (401,600] (401,600]
## [516] (401,600] (401,600] (401,600] (401,600] (401,600]
## [521] (401,600] (401,600] (401,600] (401,600] (401,600]
## [526] (401,600] (401,600] (401,600] (401,600] (401,600]
## [531] (401,600] (401,600] (401,600] (401,600] (401,600]
## [536] (401,600] (401,600] (401,600] (401,600] (401,600]
## [541] (401,600] (401,600] (401,600] (401,600] (401,600]
## [546] (401,600] (401,600] (401,600] (401,600] (401,600]
## [551] (401,600] (401,600] (401,600] (401,600] (401,600]
## [556] (401,600] (401,600] (401,600] (401,600] (401,600]
## [561] (401,600] (401,600] (401,600] (401,600] (401,600]
## [566] (401,600] (401,600] (401,600] (401,600] (401,600]
## [571] (401,600] (401,600] (401,600] (401,600] (401,600]
## [576] (401,600] (401,600] (401,600] (401,600] (401,600]
## [581] (401,600] (401,600] (401,600] (401,600] (401,600]
## [586] (401,600] (401,600] (401,600] (401,600] (401,600]
## [591] (401,600] (401,600] (401,600] (401,600] (401,600]
## [596] (401,600] (401,600] (401,600] (401,600] (401,600]
## [601] (600,800] (600,800] (600,800] (600,800] (600,800]
## [606] (600,800] (600,800] (600,800] (600,800] (600,800]
## [611] (600,800] (600,800] (600,800] (600,800] (600,800]
## [616] (600,800] (600,800] (600,800] (600,800] (600,800]
## [621] (600,800] (600,800] (600,800] (600,800] (600,800]
## [626] (600,800] (600,800] (600,800] (600,800] (600,800]
## [631] (600,800] (600,800] (600,800] (600,800] (600,800]
## [636] (600,800] (600,800] (600,800] (600,800] (600,800]
## [641] (600,800] (600,800] (600,800] (600,800] (600,800]
## [646] (600,800] (600,800] (600,800] (600,800] (600,800]
## [651] (600,800] (600,800] (600,800] (600,800] (600,800]
## [656] (600,800] (600,800] (600,800] (600,800] (600,800]
## [661] (600,800] (600,800] (600,800] (600,800] (600,800]
## [666] (600,800] (600,800] (600,800] (600,800] (600,800]
## [671] (600,800] (600,800] (600,800] (600,800] (600,800]
## [676] (600,800] (600,800] (600,800] (600,800] (600,800]
## [681] (600,800] (600,800] (600,800] (600,800] (600,800]
## [686] (600,800] (600,800] (600,800] (600,800] (600,800]
## [691] (600,800] (600,800] (600,800] (600,800] (600,800]
## [696] (600,800] (600,800] (600,800] (600,800] (600,800]
## [701] (600,800] (600,800] (600,800] (600,800] (600,800]
## [706] (600,800] (600,800] (600,800] (600,800] (600,800]
## [711] (600,800] (600,800] (600,800] (600,800] (600,800]
## [716] (600,800] (600,800] (600,800] (600,800] (600,800]
## [721] (600,800] (600,800] (600,800] (600,800] (600,800]
## [726] (600,800] (600,800] (600,800] (600,800] (600,800]
## [731] (600,800] (600,800] (600,800] (600,800] (600,800]
## [736] (600,800] (600,800] (600,800] (600,800] (600,800]
## [741] (600,800] (600,800] (600,800] (600,800] (600,800]
## [746] (600,800] (600,800] (600,800] (600,800] (600,800]
## [751] (600,800] (600,800] (600,800] (600,800] (600,800]
## [756] (600,800] (600,800] (600,800] (600,800] (600,800]
## [761] (600,800] (600,800] (600,800] (600,800] (600,800]
## [766] (600,800] (600,800] (600,800] (600,800] (600,800]
## [771] (600,800] (600,800] (600,800] (600,800] (600,800]
## [776] (600,800] (600,800] (600,800] (600,800] (600,800]
## [781] (600,800] (600,800] (600,800] (600,800] (600,800]
## [786] (600,800] (600,800] (600,800] (600,800] (600,800]
## [791] (600,800] (600,800] (600,800] (600,800] (600,800]
## [796] (600,800] (600,800] (600,800] (600,800] (600,800]
## [801] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03]
## [806] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03]
## [811] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03]
## [816] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03]
## [821] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03]
## [826] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03]
## [831] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03]
## [836] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03]
## [841] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03]
## [846] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03]
## [851] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03]
## [856] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03]
## [861] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03]
## [866] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03]
## [871] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03]
## [876] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03]
## [881] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03]
## [886] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03]
## [891] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03]
## [896] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03]
## [901] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03]
## [906] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03]
## [911] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03]
## [916] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03]
## [921] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03]
## [926] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03]
## [931] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03]
## [936] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03]
## [941] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03]
## [946] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03]
## [951] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03]
## [956] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03]
## [961] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03]
## [966] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03]
## [971] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03]
## [976] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03]
## [981] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03]
## [986] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03]
## [991] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03]
## [996] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03] (800,1e+03]
## Levels: (0.001,201] (201,401] (401,600] (600,800] (800,1e+03]
创建美观的分割点。通过选取n+1个等间距的取整值,将一个连续型变量x分割为n个区间:
x <- pretty(c(-3,3), 30)
x
## [1] -3.0 -2.8 -2.6 -2.4 -2.2 -2.0 -1.8 -1.6 -1.4 -1.2 -1.0 -0.8 -0.6 -0.4
## [15] -0.2 0.0 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4
## [29] 2.6 2.8 3.0