📜  R中向量的操作

📅  最后修改于: 2022-05-13 01:54:49.674000             🧑  作者: Mango

R中向量的操作

向量是R中最基本的数据类型。即使是创建的单个对象也以向量的形式存储。向量只不过是其他语言中定义的数组。向量包含一系列同质类型的数据。如果给出混合值,则它会根据优先级自动转换数据。可以对 R 中的向量执行各种操作。

创建向量

可以通过多种方式创建向量,如下例所示。最常见的是使用'c'函数将不同的元素组合在一起。

Python3
# Use of 'c' function
# to combine the values as a vector.
# by default the type will be double
X <- c(1, 4, 5, 2, 6, 7)
print('using c function')
print(X)
  
# using the seq() function to generate
# a sequence of continuous values
# with different step-size and length.
# length.out defines the length of vector.
Y <- seq(1, 10, length.out = 5)
print('using seq() function')
print(Y)
  
# using ':' operator to create
# a vector of continuous values.
Z <- 5:10
print('using colon')
print(Y)


Python3
# Accessing elements using the position number.
X <- c(2, 5, 8, 1, 2)
print('using Subscript operator')
print(X[2])
  
# Accessing specific values by passing
# a vector inside another vector.
Y <- c(4, 5, 2, 1, 7)
print('using c function')
print(Y[c(4, 1)])
  
# Logical indexing
Z <- c(5, 2, 1, 4, 4, 3)
print('Logical indexing')
print(Z[Z>3])


Python3
# Creating a vector
X <- c(2, 5, 1, 7, 8, 2)
  
# modify a specific element
X[3] <- 11
print('Using subscript operator')
print(X)
  
# Modify using different logics.
X[X>9] <- 0
print('Logical indexing')
print(X)
  
# Modify by specifying the position or elements.
X <- X[c(5, 2, 1)]
print('using c function')
print(X)


Python3
# Creating a vector
X <- c(5, 2, 1, 6)
  
# Deleting a vector
X <- NULL
print('Deleted vector')
print(X)


Python3
# Creating Vectors
X <- c(5, 2, 5, 1, 51, 2)
Y <- c(7, 9, 1, 5, 2, 1)
  
# Addition
Z <- X + Y
print('Addition')
print(Z)
  
# Subtraction
S <- X - Y
print('Subtraction')
print(S)
  
# Multiplication
M <- X * Y
print('Multiplication')
print(M)
  
# Division
D <- X / Y
print('Division')
print(D)


Python3
# Creating a Vector
X <- c(5, 2, 5, 1, 51, 2)
  
# Sort in ascending order
A <- sort(X)
print('sorting done in ascending order')
print(A)
  
# sort in descending order.
B <- sort(X, decreasing = TRUE)
print('sorting done in descending order')
print(B)


输出:

using c function 1 4 5 2 6 7
using seq function 1.00  3.25  5.50  7.75 10.00
using colon 5  6  7  8  9 10

访问向量元素

可以通过多种方式访问向量元素。最基本的是使用'[]',下标运算符。以下是访问 Vector 元素的方法:

Python3

# Accessing elements using the position number.
X <- c(2, 5, 8, 1, 2)
print('using Subscript operator')
print(X[2])
  
# Accessing specific values by passing
# a vector inside another vector.
Y <- c(4, 5, 2, 1, 7)
print('using c function')
print(Y[c(4, 1)])
  
# Logical indexing
Z <- c(5, 2, 1, 4, 4, 3)
print('Logical indexing')
print(Z[Z>3])

输出:

using Subscript operator 5
using c function 1 4
Logical indexing 5 4 4

修改向量

可以使用以下代码中提到的不同索引变体来修改向量:

Python3

# Creating a vector
X <- c(2, 5, 1, 7, 8, 2)
  
# modify a specific element
X[3] <- 11
print('Using subscript operator')
print(X)
  
# Modify using different logics.
X[X>9] <- 0
print('Logical indexing')
print(X)
  
# Modify by specifying the position or elements.
X <- X[c(5, 2, 1)]
print('using c function')
print(X)

输出:

Using subscript operator 2  5 11  7  8  2
Logical indexing 2 5 0 7 8 2
using c function 8 5 2

删除向量

向量可以通过将它们重新分配为 NULL 来删除。要删除向量,我们使用 NULL运算符。

Python3

# Creating a vector
X <- c(5, 2, 1, 6)
  
# Deleting a vector
X <- NULL
print('Deleted vector')
print(X)
Deleted vector NULL

算术运算

我们可以在 2 个向量之间执行算术运算。这些操作是按元素执行的,因此两个向量的长度应该相同。

Python3

# Creating Vectors
X <- c(5, 2, 5, 1, 51, 2)
Y <- c(7, 9, 1, 5, 2, 1)
  
# Addition
Z <- X + Y
print('Addition')
print(Z)
  
# Subtraction
S <- X - Y
print('Subtraction')
print(S)
  
# Multiplication
M <- X * Y
print('Multiplication')
print(M)
  
# Division
D <- X / Y
print('Division')
print(D)

输出:

Addition 12 11  6  6 53  3
Subtraction -2 -7  4 -4 49  1
Multiplication 35  18   5   5 102   2
Division 0.7142857  0.2222222  5.0000000  0.2000000 25.5000000  2.0000000

向量排序

对于排序,我们使用sort()函数,该函数默认按升序对向量进行排序。

Python3

# Creating a Vector
X <- c(5, 2, 5, 1, 51, 2)
  
# Sort in ascending order
A <- sort(X)
print('sorting done in ascending order')
print(A)
  
# sort in descending order.
B <- sort(X, decreasing = TRUE)
print('sorting done in descending order')
print(B)

输出:

sorting done in ascending order 1  2  2  5  5 51
sorting done in descending order 51  5  5  2  2  1