R语言 矢量
当涉及到数据科学、统计计算或科学研究时,R编程是最流行的语言之一。R编程在机器学习中被广泛使用,它非常高效且用户友好。它提供了灵活性,只需几行代码就能完成大型统计操作。
R语言中的矢量与C语言中的数组相同,都是用来保存相同类型的多个数据值。一个主要的关键点是,在R语言中,向量的索引将从 “1 “开始,而不是从 “0 “开始。我们也可以创建数字向量和字符向量。
向量的类型
向量有不同的类型,在R中使用。以下是一些向量的类型。
- Numeric vectors
数值向量是指那些包含数值的向量,如整数、浮点数等。
# R program to create numeric Vectors
# creation of vectors using c() function.
v1 <- c(4, 5, 6, 7)
# display type of vector
typeof(v1)
# by using 'L' we can specify that we want integer values.
v2 <- c(1L, 4L, 2L, 5L)
# display type of vector
typeof(v2)
输出:
[1] "double"
[1] "integer"
- Character vectors
字符向量包含字母数字值和特殊字符。
# R program to create Character Vectors
# by default numeric values
# are converted into characters
v1 <- c('geeks', '2', 'hello', 57)
# Displaying type of vector
typeof(v1)
输出:
[1] "character"
- Logical vectors
逻辑向量包含布尔值,如TRUE、FALSE和NA(空值)。
# R program to create Logical Vectors
# Creating logical vector
# using c() function
v1 <- c(TRUE, FALSE, TRUE, NA)
# Displaying type of vector
typeof(v1)
输出:
[1] "logical"
创建一个矢量
有不同的方法来创建矢量。一般来说,我们用’c’把不同的元素组合在一起。
# R program to create Vectors
# we can use the c function
# to combine the values as a vector.
# By default the type will be double
X <- c(61, 4, 21, 67, 89, 2)
cat('using c function', X, '\n')
# seq() function for creating
# a sequence of continuous values.
# length.out defines the length of vector.
Y <- seq(1, 10, length.out = 5)
cat('using seq() function', Y, '\n')
# use':' to create a vector
# of continuous values.
Z <- 2:7
cat('using colon', Z)
输出
using c function 61 4 21 67 89 2
using seq() function 1 3.25 5.5 7.75 10
using colon 2 3 4 5 6 7
访问矢量元素
访问矢量中的元素是指对矢量中的单个元素进行操作的过程。我们可以通过很多方法来访问向量中的元素。最常见的是使用'[]’,符号。
注意: R中的向量是基于1的索引,与普通的C、Python等格式不同。
# R program to access elements of a Vector
# accessing elements with an index number.
X <- c(2, 5, 18, 1, 12)
cat('Using Subscript operator', X[2], '\n')
# by passing a range of values
# inside the vector index.
Y <- c(4, 8, 2, 1, 17)
cat('Using combine() function', Y[c(4, 1)], '\n')
# using logical expressions
Z <- c(5, 2, 1, 4, 4, 3)
cat('Using Logical indexing', Z[Z>4])
输出
Using Subscript operator 5
Using combine() function 1 4
Using Logical indexing 5
修改一个向量
修改向量是对向量中的单个元素进行某种操作以改变其在向量中的值的过程。我们可以通过不同的方式来修改一个向量。
# R program to modify elements of a Vector
# Creating a vector
X <- c(2, 7, 9, 7, 8, 2)
# modify a specific element
X[3] <- 1
X[2] <-9
cat('subscript operator', X, '\n')
# Modify using different logics.
X[X>5] <- 0
cat('Logical indexing', X, '\n')
# Modify by specifying
# the position or elements.
X <- X[c(3, 2, 1)]
cat('combine() function', X)
输出
subscript operator 2 9 1 7 8 2
Logical indexing 2 0 1 0 0 2
combine() function 1 0 2
删除一个向量
删除一个向量是删除向量的所有元素的过程。这可以通过将其分配给一个NULL值来实现。
# R program to delete a Vector
# Creating a Vector
M <- c(8, 10, 2, 5)
# set NULL to the vector
M <- NULL
cat('Output vector', M)
输出
Output vector NULL
对矢量的元素进行排序
sort() 函数是用来帮助我们对数值进行升序或降序排序的。
# R program to sort elements of a Vector
# Creation of Vector
X <- c(8, 2, 7, 1, 11, 2)
# Sort in ascending order
A <- sort(X)
cat('ascending order', A, '\n')
# sort in descending order
# by setting decreasing as TRUE
B <- sort(X, decreasing = TRUE)
cat('descending order', B)
输出
ascending order 1 2 2 7 8 11
descending order 11 8 7 2 2 1