[英]Convert Data Frame/Matrix to list of Matrices in R
I have a long matrix (ncol=6 and nrow=50,000,000+). 我有一个长矩阵(ncol = 6和nrow = 50,000,000 +)。 I want to transform it into a list of matrices where:
我想将其转换为矩阵列表,其中:
[i,4] and [i,5] are names (or keys) of the list, [i,4]和[i,5]是列表的名称(或键),
[i,1] is the date, [i,1]是日期,
[i,2] and [i,3] are time of the day, [i,2]和[i,3]是一天中的时间,
[i,6] is the value to be stored in the list. [i,6]是要存储在列表中的值。
The matrices in the list are all be the same dimensions. 列表中的矩阵都是相同的维。
for (i in seq(1, nrow(a2015_15))) {
OD_L_2015_15[[
paste(a2015_15[i,4], a2015_15[i,5])
]] [
a2015_15[i,1], paste(a2015_15[i,2], a2015_15[i,3])
] =
as.numeric(a2015_15[i,6])
}
As is, it takes an hour or two to run which is a pain especially on a slower computer. 照原样,它需要一两个小时才能运行,特别是在速度较慢的计算机上,这是一个痛苦。
Any suggestions to make this run quicker perhaps with an apply statement? 有什么建议可以使用apply语句来加快运行速度吗? I tried to limit the calls to a2015_15 (by saving a2015_15[i,] and then referencing it) but it didn't seem to help.
我试图将调用限制为a2015_15(通过保存a2015_15 [i,]然后引用它),但这似乎没有帮助。
Thanks. 谢谢。
Edit: Here's the matrix I want to change: 编辑:这是我要更改的矩阵:
The list would be split into: OD_L_2015_15[[Key1,Key2]][Date,c(Time,Time2)]=Value 该列表将被分为:OD_L_2015_15 [[Key1,Key2]] [Date,c(Time,Time2)] =值
The assignment operation appears to be vectorizeable, ie if it works for one value of i, it should also succeed for a vector of i
's. 赋值操作似乎vectorizeable,即它是否适合我的一个价值,它也应该为一个向量成功
i
的。 . 。 Have you tried just:
您是否尝试过:
i <- seq(1,nrow(a2015_15))
OD_L_2015_15 <- list()
OD_L_2015_15[[ paste(a2015_15[i,4], a2015_15[i,5])]] [
a2015_15[i,1], paste(a2015_15[i,2], a2015_15[i,3]) ] <-
as.numeric(a2015_15[i,6])
This is untested and I am relying on your affirmation that the loop succeeds. 这未经测试,我依靠您对循环成功的确认。 You should post a smaller version of this matrix if you want tested solutions since your description has no "grounding" in "reality" at that moment.
如果您想要测试的解决方案,则应发布此矩阵的较小版本,因为此时描述在“真实性”中没有“基础”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.