1.向量是用于存储数值型、字符型、逻辑型数据的一对数组,同一向量中无法混杂不同模式的数据
2.向量创建的3种方法
①c()
-Example:

②seq(from= , to= ,by= ,length.out= ,along.with= )
-Argument
by步长,增量
length.out生成元素长度=len
along.with取赋予变量的长度为元素的个数
注:by不能与length.out连用
-Example

③rep(x,times= ,length.out= ,each= ,)
-Argument
x 任意mode的向量(数值、逻辑、字符型)
times 指定每个元素重复的次数
each x中每个元素重复的次数
-Example

3.向量索引
__-Example__

4.向量排序
①sort(x,decreasing = FALSE,na.last= NA)
对向量x进行排序,返回值排序后的数值向量
-Example

②rank(x,na.last =TRUE,ties.method=" ")
对一维数组,向量x进行排序
-Argument
na.last=NA(FALSE)删除(排在确定值之间)的缺失值
na.last=TRUE排在最后;na.last="keep"不参与排序
ties.method:针对相等的值处理方法
①"average"相同元素都取该组中的平均水平
②"first"相同元素先者在前后者在后
③"max"相同元素并列排序
④"min"相同元素都取最差水平,可以增大序列的等级差异
⑤"random"是相同元素随机编排次序
-Example

③order(x,na.last = TRUE,decreasing = FALSE)
返回x排好序的下标向量
-Example

order(X,Y)
先按照X排序,再按照Y排序,返回X的对应值位置。
5.与向量有关的函数
函数 | 用法 |
---|---|
min(x) | 最小值 |
max(x) | 最大值 |
which.min() | 显示最小值所在位置 |
which.max() | 显示最大值所在位置 |
sum(x) | 加和 |
length(x) | x的长度 |
mean(x) | 均值 |
median(x) | 中值 |
var(x) | 方差 |
sd(x) | 标准差 |
sqrt(x) | 平方根 |
abs(x) | 取绝对值 |
unique(x) | 去冗余 |
intersect(x) | 取交集 |
union(x,y) | 取并集 |
setdiff(x,y) | 差集 |
setequal() | 判断两向量是否相同(对顺序无要求) |
identical() | 判断两向量是否相同(对顺序有要求) |
网友评论