在R中,如何区分结果是向量还是矩阵?

在R中,如何区分结果是向量还是矩阵?,r,vector,matrix,R,Vector,Matrix,我现在正在学习R并使用R Studio 我写道: library(datasets) data(mtcars) ## split() function divides the data in a vector. unsplit() function do the reverse. split(mtcars$mpg, mtcars$cyl) 我回来了: $`4` [1] 22.8 24.4 22.8 32.4 30.4 33.9 21.5 27.3 26.0 30.4 21.4 $`6`

我现在正在学习R并使用R Studio

我写道:

library(datasets)
data(mtcars)

## split() function divides the data in a vector. unsplit() function do the reverse.
split(mtcars$mpg, mtcars$cyl)
我回来了:

$`4`
 [1] 22.8 24.4 22.8 32.4 30.4 33.9 21.5 27.3 26.0 30.4 21.4

$`6`
[1] 21.0 21.0 21.4 18.1 19.2 17.8 19.7

$`8`
 [1] 18.7 14.3 16.4 17.3 15.2 10.4 10.4 14.7 15.5 15.2 13.3 19.2 15.8 15.0
我知道拆分返回向量。但这是长度为1的向量吗


在R Studio中,矢量和矩阵的显示在视觉上有什么区别?

以下是一些查看拆分(计算)结果的方法。:

class(split(mtcars$mpg, mtcars$cyl))
typeof(split(mtcars$mpg, mtcars$cyl))
mode(split(mtcars$mpg, mtcars$cyl))
storage.mode(split(mtcars$mpg, mtcars$cyl))

# str() Shows the structure of the object. It gives an small summary of it.
str(split(mtcars$mpg, mtcars$cyl))
您还可以使用列表为新对象赋值,并使用前面的函数对其进行查询

cars_ls <- split(mtcars$mpg, mtcars$cyl)

class(cars_ls)
typeof(cars_ls)
mode(cars_ls)

# and

str(cars_ls)
# List of 3
# $ 4: num [1:11] 22.8 24.4 22.8 32.4 30.4 33.9 21.5 27.3 26 30.4 ...0
# $ 6: num [1:7] 21 21 21.4 18.1 19.2 17.8 19.7
# $ 8: num [1:14] 18.7 14.3 16.4 17.3 15.2 10.4 10.4 14.7 15.5 15.2 ...
编辑 从技术上讲,列表也是向量。这里还有几个函数可以检查对象的类型

is.vector(cars_ls)
# [1] TRUE
is.matrix(cars_ls)
# [1] FALSE
is.list(cars_ls)
# [1] TRUE
is.data.frame(cars_ls)
# [1] FALSE
关于未上市公司的职责:

un_ls <- unlist(cars_ls)

mode(un_ls)
storage.mode(un_ls)
typeof(un_ls)
class(un_ls)

is.vector(un_ls)
# [1] TRUE
is.list(un_ls)
# [1] FALSE

un_ls有多种
is.
功能,其中一种是

 is.matrix
您可以使用以下工具模拟is.matrix:

 is.it.a.matrix <- function(x) is.atomic(x) & length(dim(x)) == 2

is.it.a.matrix这是文档中所述的列表:
从“拆分”返回的值是包含组值的向量列表
 is.it.a.matrix <- function(x) is.atomic(x) & length(dim(x)) == 2