R查找多维数组维尺寸 [英] R Finding Multidimension Array Dimension Sizes
问题描述
一般相关问题
有人可以指出我要获取R中多维列表或数据结构的维大小的函数/方法吗?
Could someone please point me to functions / methods to get the dimension sizes of a multidimension list or datastructure in R?
了解在更大的数据结构中访问各个元素的方法也将很有用?
It would also be useful to know ways to access individual elements within this bigger datastructure?
下面有关示例输入和输出数据的问题
使用以下命令将输入数据转换为输出数据:(以下相关问题):
The Input data is converted to output data using this command:(related question below):
lst <- lapply(split(df2[-1], df2$Column_Zero), function(x)
acast(x, Column_Two~Column_One,value.var="Column_Three"))
- 创建了多少个矩阵?
- 如何访问创建的每个子矩阵的名称?
- 每个子矩阵有几列?
- 每个子矩阵有几行?
- 哪个子矩阵具有最大/最小的列数/行数?
输入数据:
Column_Zero, Column_One, Column_Two, Column_Three
XX,A, 1, 4
XX,A, 2, 3
XX,A, 3, 77
XX,B, 1, 44
XX,B, 2, 32
XX,B, 3, 770
XX,C, 1, 43
XX,C, 2, 310
XX,C, 3, 68
YY,A1, 1, 4
YY,A1, 2, 3
YY,A1, 3, 77
YY,B1, 1, 44
YY,B1, 2, 32
YY,B1, 3, 770
YY,C1, 1, 43
YY,C1, 2, 310
YY,C1, 3, 68
YY,D2, 1, 4
YY,D2, 2, 5
YY,D2, 3, 6
---------等等-----
--------- And so on -----
输出数据:
------数据表一------
------ Data Table one ------
A, B, C
4, 44, 43
3, 32, 310
77, 770, 68
------数据表二------
------ Data Table Two ------
A1, B1, C1, D2
4, 44, 43,4
3, 32, 310,5
77, 770, 68,6
--------等等-----
------ and so on -----
相关问题: 这是在相关的问题中提出的,我们将在其中拆分数据表并对其进行重塑.因此,了解较小的数据结构/矩阵或数据表的大小变得很重要.
Related Question: This comes up in the related question, where we are splitting a data table and reshaping it. So it becomes important to know how big the smaller data structures / matrices or data tables are.
如果有任何不清楚的地方或需要进一步的信息,请告诉我.
Please let me know if anything is not clear or if you need any further information.
推荐答案
问题的答案是
- 创建了多少个矩阵?
由于每个list
元素都是matrix
,因此我们可以使用length
来查找matrices
As each of the list
element is a matrix
, we can use length
to find the number of matrices
length(lst)
#[1] 2
(在一般情况下)如果还有其他对象
In case if there are other objects (in a general scenario)
sum(sapply(lst, is.matrix))
#[1] 2
- 如何访问创建的每个子矩阵的名称?
我们可以使用names
names(lst)
- 每个子矩阵有几列?
我们可以通过遍历lst
sapply(lst, ncol)
# XX YY
# 3 4
- 每个子矩阵有几行?
与上述相同,将ncol
替换为nrow
.我们可以同时使用dim
It is the same as above by replacing ncol
with nrow
. We can get both by using dim
lapply(lst, dim)
- 哪个子矩阵具有最大/最小的列数/行数?
我们可以在遍历"lst"之后使用which.min
或which.max
查找索引.
We can use which.min
or which.max
to find the index after looping through the 'lst'.
lst[which.min(sapply(lst, ncol) )]
lst[which.max(sapply(lst, ncol))]
和nrow
这篇关于R查找多维数组维尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!