R语言数字类型 #
一、数字类型概述 #
R语言中的数字类型主要包括:
| 类型 | 说明 | 示例 |
|---|---|---|
| integer | 整数 | 1L, 100L |
| double | 双精度浮点数 | 3.14, 1.0 |
| complex | 复数 | 3+2i |
二、整数类型 #
2.1 创建整数 #
r
x <- 100L
class(x)
typeof(x)
y <- as.integer(100)
class(y)
2.2 整数范围 #
r
.Machine$integer.max
as.integer(2^31 - 1)
as.integer(2^31)
2.3 整数运算 #
r
x <- 10L
y <- 3L
x + y
x - y
x * y
x %/% y
x %% y
三、浮点数类型 #
3.1 创建浮点数 #
r
x <- 3.14
class(x)
typeof(x)
y <- 100
class(y)
3.2 科学计数法 #
r
x <- 1.5e10
y <- 2.5e-5
print(x)
print(y)
3.3 浮点数精度 #
r
.Machine$double.eps
.Machine$double.xmax
.Machine$double.xmin
0.1 + 0.2 == 0.3
all.equal(0.1 + 0.2, 0.3)
3.4 浮点数运算 #
r
x <- 10.5
y <- 3.2
x + y
x - y
x * y
x / y
x ^ 2
sqrt(x)
四、复数类型 #
4.1 创建复数 #
r
x <- 3 + 2i
class(x)
typeof(x)
y <- complex(real = 3, imaginary = 2)
z <- complex(real = c(1, 2), imaginary = c(3, 4))
4.2 复数运算 #
r
x <- 3 + 2i
y <- 1 + 1i
x + y
x - y
x * y
x / y
4.3 复数函数 #
r
x <- 3 + 4i
Re(x)
Im(x)
Mod(x)
Arg(x)
Conj(x)
五、数学运算 #
5.1 基本运算 #
r
x <- 10
y <- 3
x + y
x - y
x * y
x / y
x ^ y
x %% y
x %/% y
5.2 数学函数 #
r
x <- -3.7
abs(x)
sqrt(16)
exp(1)
log(10)
log10(100)
log2(8)
5.3 取整函数 #
r
x <- 3.7
round(x)
round(x, 1)
ceiling(x)
floor(x)
trunc(x)
signif(3.14159, 3)
5.4 三角函数 #
r
x <- pi / 4
sin(x)
cos(x)
tan(x)
asin(0.5)
acos(0.5)
atan(1)
5.5 双曲函数 #
r
x <- 1
sinh(x)
cosh(x)
tanh(x)
asinh(x)
acosh(x)
atanh(0.5)
六、统计函数 #
6.1 基本统计 #
r
x <- c(1, 2, 3, 4, 5)
sum(x)
mean(x)
median(x)
sd(x)
var(x)
6.2 范围函数 #
r
x <- c(3, 1, 4, 1, 5, 9, 2, 6)
min(x)
max(x)
range(x)
6.3 累积函数 #
r
x <- c(1, 2, 3, 4, 5)
cumsum(x)
cumprod(x)
cummax(x)
cummin(x)
6.4 排序函数 #
r
x <- c(3, 1, 4, 1, 5, 9, 2, 6)
sort(x)
order(x)
rank(x)
rev(x)
七、随机数 #
7.1 均匀分布 #
r
runif(5)
runif(5, min = 0, max = 10)
sample(1:100, 5)
sample(1:10, 5, replace = TRUE)
7.2 正态分布 #
r
rnorm(5)
rnorm(5, mean = 10, sd = 2)
set.seed(123)
rnorm(5)
7.3 其他分布 #
r
rbinom(10, size = 10, prob = 0.5)
rpois(10, lambda = 5)
rexp(10, rate = 1)
rgamma(10, shape = 2)
八、类型转换 #
8.1 转换函数 #
r
x <- 3.7
as.integer(x)
as.numeric(x)
as.double(x)
as.complex(x)
8.2 类型检查 #
r
x <- 3.14
is.numeric(x)
is.integer(x)
is.double(x)
is.complex(x)
8.3 类型强制转换 #
r
x <- c(1L, 2.5, 3L)
class(x)
y <- c(1, 2, "3")
class(y)
九、特殊值处理 #
9.1 无穷大 #
r
x <- 1 / 0
x
is.infinite(x)
is.finite(x)
9.2 NaN #
r
x <- 0 / 0
x
is.nan(x)
9.3 处理特殊值 #
r
x <- c(1, Inf, -Inf, NaN, NA)
is.finite(x)
is.infinite(x)
is.nan(x)
is.na(x)
x[is.finite(x)]
十、数值比较 #
10.1 精确比较 #
r
x <- 0.1 + 0.2
y <- 0.3
x == y
all.equal(x, y)
10.2 范围比较 #
r
x <- 5
x > 0 && x < 10
x >= 1 && x <= 10
10.3 差值比较 #
r
x <- 0.1 + 0.2
y <- 0.3
abs(x - y) < 1e-10
十一、实践示例 #
11.1 计算圆的面积 #
r
radius <- 5
area <- pi * radius ^ 2
print(paste("圆的面积:", round(area, 2)))
11.2 温度转换 #
r
celsius_to_fahrenheit <- function(celsius) {
fahrenheit <- celsius * 9 / 5 + 32
return(fahrenheit)
}
celsius_to_fahrenheit(25)
11.3 统计计算 #
r
scores <- c(85, 90, 78, 92, 88, 76, 95, 82)
cat("平均分:", mean(scores), "\n")
cat("标准差:", round(sd(scores), 2), "\n")
cat("最高分:", max(scores), "\n")
cat("最低分:", min(scores), "\n")
十二、总结 #
本章学习了:
- 整数类型的创建和运算
- 浮点数类型和精度问题
- 复数类型和运算
- 各种数学函数
- 统计函数的使用
- 随机数生成
- 类型转换和检查
- 特殊值处理
掌握数字类型是进行数据分析和统计计算的基础!
最后更新:2026-03-27