还没整理好,再慢慢搞吧。
1. 数据结构
1.1 标量
1.1.1 数值型
is.numeric()
as.numeric()
-
abs(x)
: 绝对值 -
sqrt(x)
: 平方根 -
ceiling(x)
: 不小于x
的最小整数 -
floor(x)
: 不大于x
的最大整数 -
round(x, digits = n)
: 将x
舍入为指定的位的小数 -
trunc(5.2)
: 向0的方向截取的x中的整数部分 -
signif(x, digits = 2)
: 将x
舍入为指定的有效数字位数
> is.numeric()
> as.numeric()
1.1.2 字符型
> hi <- "hello world!"
> print(hi)
1.1.3 逻辑型
> is.numeric(TRUE)
> is.numeric("TRUE")
1.2 向量
1.2.1 创建
-
c()
: 创建向量,单个向量中的元素必须拥有相同的类型或模式 seq(from = 1, to, by)
-
rep()
times
each
paste()
> paste(c("a", "b"), 1:4, sep = "---")
[1] "a---1" "b---2" "a---3" "b---4"
-
sample(x, size, replace = FALSE)
、sample.int()
replace = TRUE
: 可放回抽样 runif(n, min = 0, max = 1)
> runif(10, 1, 100)
[1] 33.72245 20.66923 24.33374 28.21378 59.54078 26.08567 13.22524
[8] 23.76068 60.15995 21.92945
-
unique()
: 返回一个删除了重复元素或行的向量、数据框或数组 -
duplicated()
: 查找在同一向量中有重复的记录
> a = c(1,2,3,4,5,1,2,3)
> unique(a)
[1] 1 2 3 4 5
> duplicated(a)
[1] FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE
> a[duplicated(a)]
[1] 1 2 3
1.2.2 取值
-
which()
: 根据条件筛选 grep()
grepl()
> number <- 1:5
> a[1] # 按索引取值
> a[-1]`
> a[c(1,3,5)]
>
> TF <- number > 3
> TF
[1] FALSE FALSE FALSE TRUE TRUE
> number[TF] # 按逻辑取值
[1] 4 5
1.2.3 计算
max()
min()
mean()
median()
sum()
-
cumsum()
: 累积求和 -
var()
: 方差 -
sd()
: 标准差 -
range()
: 值域 -
cor()
: 相关性 -
lm()
: 线性回归
> x <- 1:10
> cumsum(x)
[1] 1 3 6 10 15 21 28 36 45 55
> lm(cumsum(x) ~ x)
Call:
lm(formula = cumsum(x) ~ x)
Coefficients:
(Intercept) x
-11 6
> range(x)
[1] 1 10
> which.max(x)
[1] 10
> cor(x, cumsum(x))
[1] 0.9784921
1.3 因子
factor()
levels()
-
cut()
: 连续向量的离散化
1.4 矩阵
二维,每个元素都拥有相同的模式。
-
matrix()
ncol
: 设置行数
nrow
: 设置列数
dimnames
: 行名
byrow
: 是否按行填充
matrix(sample.int(100,15*20,replace = T), nrow = 20, ncol = 15)
1.5 数据框
1.5.1 创建
-
data.frame()
stringsAsFactors
1.5.2 提取
df[1:500,]
subset()
complete.cases()
1.5.3 合并
-
cbind()
: 按列合并,要求行数相等 -
rbind()
: 按行合并,要求列数是相同
1.5.4 排序
-
sort()
: 对向量做简单的排序
index.return
: 逻辑型,是否返回排序的索引
decreasing
: 逻辑型,是否降序处理 -
order()
: 返回排完序之后的数据在排序之前的位置
1.5.5 变换
-
t()
: 转置 -
reshape::2dcast()
: 变换 -
reshape::melt()
: 变换
1.6 列表
split()
unsplit()
1.7 批量操作
sapply()
map()
with()
by()
aggregate()
2. 数据查看
2.1 大体
-
mode()
: 显示某个对象的模式 typeof()
class()
-
str()
: 显示某个对象的类或类型 glimpse()
-
summary()
: 获取描述性统计量,最小值、最大值、四分位数......
2.2 行列维度
-
dim()
: 显示某个对象的维度 -
length()
: 显示对象中元素/成分的数量 -
nrow()
: 获取行数 -
ncol()
: 获取列数 -
names()
: 获取/设置列名 -
colnames()
: 获取/设置列名 -
rownames()
: 获取/设置行名 head()
tail()
some()
2.3 编辑器
view()
edit()
fix()
3. 工作目录
3.1 常用
-
getwd()
: 获取当前工作目录 -
setwd()
: 设置当前工作目录 -
ls()
: 列出当前工作空间中的对象 -
rm()
: 移除一个或多个对象 -
q()
: 退出R
,将会询问你是否保存工作空间 options()
> rm(ls())
3.2 数据导入
-
scan()
: 将数据读作一行向量形式 -
readChar()
: 读入字符型数据
-
read.table()
header
: 逻辑,是否以第一行为表头
sep
: 分隔符
row.names
: 设置某列为行名 read.csv()
read.delim()
readfwf()
readLines()
-
attach()
: 把数据框的变量连接到内存中 detach
fread()
3.3 数据导出
write.table()
write.csv()
fwrite()
-
sink()
: 将文本输出重定向到文件中 -
pdf(); dev.off()
: 将输出返回到终端
3.4 其他
-
history(n)
: 显示最近使用过的n
个命令(默认25
个) -
savehistory("myfile")
: 保存命令历史到文件myfile
中 -
loadhistory("myfile")
: 载入一个命令历史文件(默认为.Rhistory
) -
save.imge("myfile")
: 保存工作空间到文件myfile
中(默认值为.RData
) -
save("myfile")
: 保存指定对象到一个文件中 -
load("myfile")
: 读取一个工作空间到当前会话中(默认值为.RData
)
4. 自定义函数
function()
5. 控制流
......
参考
书 | 《R语言实战》
网友评论