R语言 把一个对象转换成一个矢量 – as.vector() 函数
R语言中的 as.vector() 函数用于将一个对象转换成一个矢量。
语法: as.vector(x)
参数:
x: 要转换的对象
例1 :
# R program to convert an object to vector
# Creating an array
x <- array(c(2, 3, 4, 7, 2, 5), c(3, 2))
x
# Calling as.vector() Function
as.vector(x)
输出
[, 1] [, 2]
[1, ] 2 7
[2, ] 3 2
[3, ] 4 5
[1] 2 3 4 7 2 5
例2 :
# R program to convert an object to vector
# Creating a matrix
x <- matrix(c(1:9), 3, 3)
x
# Calling as.vector() Function
as.vector(x)
输出
[, 1] [, 2] [, 3]
[1, ] 1 4 7
[2, ] 2 5 8
[3, ] 3 6 9
[1] 1 2 3 4 5 6 7 8 9