R 向量

向量是最基本的R语言数据对象,有六种类型的原子向量。 它们是逻辑,整数,双精度,复杂,字符和原始。

创建向量

单元素向量

即使在R语言中只写入一个值,它也将成为长度为1的向量,并且属于上述向量类型之一。

  1. # Atomic vector of type character.
  2. print("abc");
  3. # Atomic vector of type double.
  4. print(12.5)
  5. # Atomic vector of type integer.
  6. print(63L)
  7. # Atomic vector of type logical.
  8. print(TRUE)
  9. # Atomic vector of type complex.
  10. print(2+3i)
  11. # Atomic vector of type raw.
  12. print(charToRaw('hello'))

当我们执行上面的代码,它产生以下结果:

  1. [1] "abc"
  2. [1] 12.5
  3. [1] 63
  4. [1] TRUE
  5. [1] 2+3i
  6. [1] 68 65 6c 6c 6f

多元素向量

对数值数据使用冒号运算符

  1. # Creating a sequence from 5 to 13.
  2. v <- 5:13
  3. print(v)
  4. # Creating a sequence from 6.6 to 12.6.
  5. v <- 6.6:12.6
  6. print(v)
  7. # If the final element specified does not belong to the sequence then it is discarded.
  8. v <- 3.8:11.4
  9. print(v)

当我们执行上面的代码,它产生以下结果:

  1. [1] 5 6 7 8 9 10 11 12 13
  2. [1] 6.6 7.6 8.6 9.6 10.6 11.6 12.6
  3. [1] 3.8 4.8 5.8 6.8 7.8 8.8 9.8 10.8

使用sequence (Seq.)序列运算符

  1. # Create vector with elements from 5 to 9 incrementing by 0.4.
  2. print(seq(5, 9, by = 0.4))

当我们执行上面的代码,它产生以下结果:

  1. [1] 5.0 5.4 5.8 6.2 6.6 7.0 7.4 7.8 8.2 8.6 9.0

使用C()函数

如果其中一个元素是字符,则非字符值被强制转换为字符类型。

  1. # The logical and numeric values are converted to characters.
  2. s <- c('apple','red',5,TRUE)
  3. print(s)

当我们执行上面的代码,它产生以下结果:

  1. [1] "apple" "red" "5" "TRUE"

访问向量元素

使用索引访问向量的元素。 []括号用于建立索引。 索引从位置1开始。在索引中给出负值会丢弃来自result.TRUEFALSE或0和1的元素,也可用于索引。

  1. # Accessing vector elements using position.
  2. t <- c("Sun","Mon","Tue","Wed","Thurs","Fri","Sat")
  3. u <- t[c(2,3,6)]
  4. print(u)
  5. # Accessing vector elements using logical indexing.
  6. v <- t[c(TRUE,FALSE,FALSE,FALSE,FALSE,TRUE,FALSE)]
  7. print(v)
  8. # Accessing vector elements using negative indexing.
  9. x <- t[c(-2,-5)]
  10. print(x)
  11. # Accessing vector elements using 0/1 indexing.
  12. y <- t[c(0,0,0,0,0,0,1)]
  13. print(y)

当我们执行上面的代码,它产生以下结果:

  1. [1] "Mon" "Tue" "Fri"
  2. [1] "Sun" "Fri"
  3. [1] "Sun" "Tue" "Wed" "Fri" "Sat"
  4. [1] "Sun"

向量操作

向量运算

可以添加,减去,相乘或相除两个相同长度的向量,将结果作为向量输出。

  1. # Create two vectors.
  2. v1 <- c(3,8,4,5,0,11)
  3. v2 <- c(4,11,0,8,1,2)
  4. # Vector addition.
  5. add.result <- v1+v2
  6. print(add.result)
  7. # Vector substraction.
  8. sub.result <- v1-v2
  9. print(sub.result)
  10. # Vector multiplication.
  11. multi.result <- v1*v2
  12. print(multi.result)
  13. # Vector division.
  14. divi.result <- v1/v2
  15. print(divi.result)

当我们执行上面的代码,它产生以下结果:

  1. [1] 7 19 4 13 1 13
  2. [1] -1 -3 4 -3 -1 9
  3. [1] 12 88 0 40 0 22
  4. [1] 0.7500000 0.7272727 Inf 0.6250000 0.0000000 5.5000000

向量元素回收

如果我们对不等长的两个向量应用算术运算,则较短向量的元素被循环以完成操作。

  1. v1 <- c(3,8,4,5,0,11)
  2. v2 <- c(4,11)
  3. # V2 becomes c(4,11,4,11,4,11)
  4. add.result <- v1+v2
  5. print(add.result)
  6. sub.result <- v1-v2
  7. print(sub.result)

当我们执行上面的代码,它产生以下结果:

  1. [1] 7 19 8 16 4 22
  2. [1] -1 -3 0 -6 -4 0

向量元素排序

向量中的元素可以使用sort()函数排序。

  1. v <- c(3,8,4,5,0,11, -9, 304)
  2. # Sort the elements of the vector.
  3. sort.result <- sort(v)
  4. print(sort.result)
  5. # Sort the elements in the reverse order.
  6. revsort.result <- sort(v, decreasing = TRUE)
  7. print(revsort.result)
  8. # Sorting character vectors.
  9. v <- c("Red","Blue","yellow","violet")
  10. sort.result <- sort(v)
  11. print(sort.result)
  12. # Sorting character vectors in reverse order.
  13. revsort.result <- sort(v, decreasing = TRUE)
  14. print(revsort.result)

当我们执行上面的代码,它产生以下结果:

  1. [1] -9 0 3 4 5 8 11 304
  2. [1] 304 11 8 5 4 3 0 -9
  3. [1] "Blue" "Red" "violet" "yellow"
  4. [1] "yellow" "violet" "Red" "Blue"