R矩阵结构-操作问题 [英] R matrix structure - manipulation issue

查看:77
本文介绍了R矩阵结构-操作问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解决 R 程序上的两个主要问题.

I'm trying to resolve two main issues on an R program.

首先,我需要创建一种矩阵结构来为不同的个人(行)存储不同变量(列)的值.

First I need to create a kind-of matrix structure to store for different individuals (rows) the values of different variables (columns).

主要问题是,在每一对行列中,我都需要有一个时间序列形式的观察向量.因为每个不同变量的时间序列不是按顺序采样的,所以不同的时间序列不是规则的.

The main problem is that in each pair row-column I need to have a vector of observations in the form of a time-series. Because the time series in each different variable aren't sampled at sequential times the different time series aren't regular.

我正在考虑使用行名 colnames 作为访问矩阵中各项的键.

I'm thinking of using the rownames and colnames as keys for accessing the items in the matrix.

不知道这是否是我所需的最佳结构.使用数据帧,但建议移至矩阵,因为它应存储大量数据(数据流).

Don't know if this is the best structure for what I need. Was using dataframes but had the suggestion to move to matrix because it should store a bit of big amounts of data (datastream).

推荐答案

您可以将单个时间序列存储在列表中,例如l,并在其上设置dim属性.您也可以设置dimnames(即行名和列名).有了它,您几乎可以像使用matrix/data.frame

You can store individual time series in a list, say l, and set dim attribute on it. You can set dimnames (i.e. row names and column names) too. With that you can use it almost as if it is a matrix/data.frame

# Generate length 15 vectors for 10 subjects
l <- replicate(10, list(rnorm(15)))
dim(l) <- c(5, 2)
dimnames(l) <- list(subject=1:5, variable=c("a", "b"))
l
##        variable
## subject a          b         
##       1 Numeric,15 Numeric,15
##       2 Numeric,15 Numeric,15
##       3 Numeric,15 Numeric,15
##       4 Numeric,15 Numeric,15
##       5 Numeric,15 Numeric,15

现在您可以:

l[[1,1]]    # time series for subject 1, var 1
##  [1] -0.02425  0.88986  0.36260 -1.78774 -1.48874 -1.46750  0.38329
##  [8]  0.18573 -1.65675  0.59374  0.81669  1.06867 -1.71847  0.81889
## [15]  0.10796

l[[2, "b"]] # time series for subject 2, var "b"
##  [1]  0.45616 -0.67563 -1.42116 -0.42621  0.51648  0.35147  0.68243
##  [8]  1.17581 -0.16696  0.77492 -1.76446  1.50580  0.06075  0.37734
## [15] -0.92797

等...

这篇关于R矩阵结构-操作问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆