R - 将矩阵存储到单个数据帧单元中 [英] R - store a matrix into a single dataframe cell

查看:137
本文介绍了R - 将矩阵存储到单个数据帧单元中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将整个矩阵/数组存储到数据帧的单个单元格中,但不能完全记住如何执行。



现在,在你说不能完成之前,我确信我记得有人在这个问题上提出了一个问题,虽然这不是问题,所以我找不到它。



例如,您可以在矩阵的单个单元格中存储矩阵,如下所示:

  myMat<  -  array(list(),dim = c(2,2))
myMat [[1,1]]< 1:5
myMat [[1,2]]< - 6:10

#[,1] [,2]
#[1,]整数,5整数,5
#[2,] NULL NULL

诀窍是使用双括号 [[]]



现在我只是无法解决如何做一个数据框(或如果可以):

 #尝试制作如上所述的数据框(除非我使用list()它获得
#解释为m列没有存在)
myDF< - data.frame(i = 1:5,m = NA)
myDF [[1,'m']]< - 1:5
# `[[< - 。data.frame`(`* tmp *`,1,m,value = 1:5)中的错误:
#提供的元素要比替换

#这似乎工作,但我必须做myDF $ m [[1]] [[1]]得到1:5,
#,而我只是想做myDF $ m [[ 1]]。
myDF [[1,'m']]< - list(1:5)



<我想我几乎在那里最后一次尝试,我可以执行 myDF [[1,'m']] 以检索列表(1:5)因此 myDF [[1,'m']] [[1]] 获得 1:5 但是我宁愿只做 myDF [[1,'m']] ,并获得 1:5

解决方案

我认为诀窍可能是将其插入到列表中:


$ b $ ($)$($)$($)
dat < - data.frame(women,m = I(replicate(nrow(women)),matrix(rnorm ,2,2),
simplified = FALSE))


str(dat)
'data.frame':15 obs。的3个变量:
$ height:num 58 59 60 61 62 63 64 65 66 67 ...
$ weight:num 115 117 120 123 126 129 132 135 139 142 ...
$ m:列表15
.. $:num [1:2,1:2] -0.5605 -0.2302 1.5587 0.0705
.. $:num [1:2,1:2] 0.129 1.715 0.461 -1.265
...
.. $:num [1:2,1:2] -1.549 0.585 0.124 0.216
..- attr(*,class)= chr AsIs

dat [[1,m]]
[,1] [,2]
[1,] -0.5604756 1.55870831
[2 ,] -0.2301775 0.07050839

dat [[2,m]]
[,1] [,2]
[1,] 0.1292877 0.4609162
[ 2,] 1.7150650 -1.2650612

编辑:所以问题的确是初始化,然后分配。鉴于此,您应该可以像您这样的问题一样定义一个数据框架:

  data.frame i = 1:5,m = I(vector(mode =list,length = 5)))

您可以这样分配:

  dat [[2,m]]<  - 矩阵(rnorm(9),3,3)


I'm trying to store an entire matrix/array into a single cell of a data frame, but can't quite remember how to do it.

Now before you say it can't be done, I'm sure I remember someone asking a question on SO where it was done, although that wasn't the point of the question so I can't find it again.

For example, you can store matrices inti a single cell of a matrix like so:

myMat <- array(list(), dim=c(2, 2))
myMat[[1, 1]] <- 1:5
myMat[[1, 2]] <- 6:10

#     [,1]      [,2]     
#[1,] Integer,5 Integer,5
#[2,] NULL      NULL

The trick was in using the double brackets [[]].

Now I just can't work out how to do it for a data frame (or if you can):

# attempt to make a dataframe like above (except if I use list() it gets
# interpreted to mean the `m` column doesn't exist)
myDF <- data.frame(i=1:5, m=NA)
myDF[[1, 'm']] <- 1:5
# Error in `[[<-.data.frame`(`*tmp*`, 1, "m", value = 1:5) : 
#  more elements supplied than there are to replace

# this seems to work but I have to do myDF$m[[1]][[1]] to get the 1:5,
# whereas I just want to do myDF$m[[1]].
myDF[[1, 'm']] <- list(1:5)

I think I'm almost there. With that last attempt I can do myDF[[1, 'm']] to retrieve list(1:5) and hence myDF[[1, 'm']][[1]] to get 1:5, but I'd prefer to just do myDF[[1, 'm']] and get 1:5.

解决方案

I think the trick may be to insert it in as a list:

set.seed(123)
dat <- data.frame(women, m=I(replicate(nrow(women), matrix(rnorm(4), 2, 2), 
                simplify=FALSE)))


str(dat)
'data.frame':   15 obs. of  3 variables:
 $ height: num  58 59 60 61 62 63 64 65 66 67 ...
 $ weight: num  115 117 120 123 126 129 132 135 139 142 ...
 $ m     :List of 15
  ..$ : num [1:2, 1:2] -0.5605 -0.2302 1.5587 0.0705
  ..$ : num [1:2, 1:2] 0.129 1.715 0.461 -1.265
  ...
  ..$ : num [1:2, 1:2] -1.549 0.585 0.124 0.216
  ..- attr(*, "class")= chr "AsIs"

dat[[1, "m"]]
           [,1]       [,2]
[1,] -0.5604756 1.55870831
[2,] -0.2301775 0.07050839

dat[[2, "m"]]
          [,1]       [,2]
[1,] 0.1292877  0.4609162
[2,] 1.7150650 -1.2650612

EDIT: So the question really is about initialising and then assigning. Given that, you should be able to define a data.frame like the one in your question like so:

data.frame(i=1:5, m=I(vector(mode="list", length=5)))

You can then assign to it like so:

dat[[2, "m"]] <- matrix(rnorm(9), 3, 3)

这篇关于R - 将矩阵存储到单个数据帧单元中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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