R语言 向量
向量是最基本的R数据对象,共有六种类型的原子向量。它们是逻辑型、整型、双精度型、复数型、字符型和原始型。
向量的创建
单元素向量
即使在R中只写一个值,它也会成为长度为1的向量,并属于以上某种向量类型。
# Atomic vector of type character.
print("abc");
# Atomic vector of type double.
print(12.5)
# Atomic vector of type integer.
print(63L)
# Atomic vector of type logical.
print(TRUE)
# Atomic vector of type complex.
print(2+3i)
# Atomic vector of type raw.
print(charToRaw('hello'))
当我们执行上面的代码时,会产生以下结果 –
[1] "abc"
[1] 12.5
[1] 63
[1] TRUE
[1] 2+3i
[1] 68 65 6c 6c 6f
多元素向量
使用冒号运算符处理数字数据
# Creating a sequence from 5 to 13.
v <- 5:13
print(v)
# Creating a sequence from 6.6 to 12.6.
v <- 6.6:12.6
print(v)
# If the final element specified does not belong to the sequence then it is discarded.
v <- 3.8:11.4
print(v)
当我们执行上面的代码时,会产生以下结果 −
[1] 5 6 7 8 9 10 11 12 13
[1] 6.6 7.6 8.6 9.6 10.6 11.6 12.6
[1] 3.8 4.8 5.8 6.8 7.8 8.8 9.8 10.8
使用序列(Seq.)运算符
# Create vector with elements from 5 to 9 incrementing by 0.4.
print(seq(5, 9, by = 0.4))
当我们执行上面的代码时,会产生如下结果-
[1] 5.0 5.4 5.8 6.2 6.6 7.0 7.4 7.8 8.2 8.6 9.0
使用c()函数
如果其中一个元素是字符类型,那么非字符值将被强制转换为字符类型。
# The logical and numeric values are converted to characters.
s <- c('apple','red',5,TRUE)
print(s)
当我们执行上述代码时,将产生以下结果−
[1] "apple" "red" "5" "TRUE"
访问向量元素
使用索引访问向量的元素。索引使用 [ ]括号 。索引从位置1开始。在索引中给出负值会从结果中删除该元素。 TRUE , FALSE 或 0 和 1 也可以用于索引。
# Accessing vector elements using position.
t <- c("Sun","Mon","Tue","Wed","Thurs","Fri","Sat")
u <- t[c(2,3,6)]
print(u)
# Accessing vector elements using logical indexing.
v <- t[c(TRUE,FALSE,FALSE,FALSE,FALSE,TRUE,FALSE)]
print(v)
# Accessing vector elements using negative indexing.
x <- t[c(-2,-5)]
print(x)
# Accessing vector elements using 0/1 indexing.
y <- t[c(0,0,0,0,0,0,1)]
print(y)
当我们执行上述代码时,会产生以下结果−
[1] "Mon" "Tue" "Fri"
[1] "Sun" "Fri"
[1] "Sun" "Tue" "Wed" "Fri" "Sat"
[1] "Sun"
向量操作
向量的运算
两个长度相同的向量可以进行加法、减法、乘法或除法运算,结果为向量输出。
# Create two vectors.
v1 <- c(3,8,4,5,0,11)
v2 <- c(4,11,0,8,1,2)
# Vector addition.
add.result <- v1+v2
print(add.result)
# Vector subtraction.
sub.result <- v1-v2
print(sub.result)
# Vector multiplication.
multi.result <- v1*v2
print(multi.result)
# Vector division.
divi.result <- v1/v2
print(divi.result)
当我们执行上面的代码时,它会产生以下结果−
[1] 7 19 4 13 1 13
[1] -1 -3 4 -3 -1 9
[1] 12 88 0 40 0 22
[1] 0.7500000 0.7272727 Inf 0.6250000 0.0000000 5.5000000
向量元素循环利用
如果我们对两个长度不相等的向量应用算术运算,那么较短向量的元素将被循环利用来完成运算。
v1 <- c(3,8,4,5,0,11)
v2 <- c(4,11)
# V2 becomes c(4,11,4,11,4,11)
add.result <- v1+v2
print(add.result)
sub.result <- v1-v2
print(sub.result)
当我们执行上面的代码时,它会产生以下结果 −
[1] 7 19 8 16 4 22
[1] -1 -3 0 -6 -4 0
矢量元素排序
可以使用 sort() 函数对矢量中的元素进行排序。
v <- c(3,8,4,5,0,11, -9, 304)
# Sort the elements of the vector.
sort.result <- sort(v)
print(sort.result)
# Sort the elements in the reverse order.
revsort.result <- sort(v, decreasing = TRUE)
print(revsort.result)
# Sorting character vectors.
v <- c("Red","Blue","yellow","violet")
sort.result <- sort(v)
print(sort.result)
# Sorting character vectors in reverse order.
revsort.result <- sort(v, decreasing = TRUE)
print(revsort.result)
当我们执行上述代码时,将会产生以下结果 –
[1] -9 0 3 4 5 8 11 304
[1] 304 11 8 5 4 3 0 -9
[1] "Blue" "Red" "violet" "yellow"
[1] "yellow" "violet" "Red" "Blue"