如何调整R中较小的数据结构? [英] How to adjust this data structure for smaller segments in R?
问题描述
当前数据结构
[1:541650,1:4]
其中四个信号长度为541650,我想转换为数据结构 [1:360,1:4 * 1505]
或类似的情况,我为数据结构创建了多余的空格,因为 1:4 * 1504
将丢失一些尾点 >>> 541650 * 4.0
2166600.0
>>> 360 * 1505 * 4
2167200.0
当前数据结构,当前代码及其内容在R
m1 < - 矩阵(1:541650,ncol = 4,nrow = 541650); str(m1)
#int [1:541650,1:4] 1 2 3 4 5 6 7 8 9 10 ...
#case:num [1:541650,1:4] 0.675 -0.67 -0.67 -0.65 -0.65 -0.6 -0.555 -0.535 -0.52 -0.515 ...
对当前数据结构的测试功能: M.ecg.cor< - cor(M.ecg)
当前输出:4x4矩阵
使用示例
$ b测试akrun的预期输出:6020x6020矩阵
R:3.3.1
操作系统:Debian 8.5
将会转换成一个 array
,但数组
只能保留固定的维度。所以,如果我们缺少元素数量,最后添加一些NAs,然后转换为3D数组。
m2 < - array(`length< -`(m1,30),dim = c(2,5,3))
然后应用
该函数通过指定 MARGIN
为3。
res< - apply(m2,3,FUN = function(x)list(cor(x)))
identical(res [ [1]] [[1]],cor(m2 [,, 1])$ b $ b#[1] TRUE
或另一个选项是使用 lapply
循环遍历第三维,并应用 cor
res2< - lapply(seq(dim(m2)[3]),function(i)cor(m2 [ ,i]))
数据
set.seed(24)
/ pre>
m1 < - matrix(rnorm(45),ncol = 5,nrow = 9)
I have four complete signals in the following datastructure. I would like to split each signal to 360 blocks or close to it. Current data structure
[1:541650, 1:4]
where four signals of the length 541650, which I want to convert to the data structure[1:360, 1:4*1505]
or similar where I created excess spaces for the data structure because1:4*1504
would lose some tail points>>> 541650*4.0 2166600.0 >>> 360*1505*4 2167200.0
Current data structure, current code and its contents in R
m1 <- matrix(1:541650, ncol=4, nrow=541650); str(m1) #int [1:541650, 1:4] 1 2 3 4 5 6 7 8 9 10 ... #case: num [1:541650, 1:4] -0.675 -0.67 -0.67 -0.65 -0.65 -0.6 -0.555 -0.535 -0.52 -0.515 ...
Test function to the current data structure:
M.ecg.cor <- cor(M.ecg)
Current output: 4x4 matrixTesting akrun's answer with the case example
Code
# http://stackoverflow.com/q/40429343/54964 library("corrgram") set.seed(24) A=541650 m1 <- matrix(1:A, ncol=4, nrow=A) a=360; b=1505; c=4; # http://stackoverflow.com/a/40430229/54964 m2 <- array(`length<-`(m1, a*b*c), dim = c(a,b,c)) res <- lapply(seq(dim(m2)[3]), function(i) cor(m2[,,i])) str(res) res2 <- lapply(res, function(x) eigen(replace(x, is.na(x), 0))$vectors[,1:2]) str(res2) res2 <- do.call(rbind, res2) # a single matrix dim(res2) # 6020 2 # Not Passed because output strange corrgram(res2, upper.panel=panel.pie, lower.panel=panel.shade, text.panel=panel.txt, order=NULL, diag.panel=panel.minmax)
Output, Fig. 1 Output is only 1x1 matrix
List of 4 $ : num [1:1505, 1:1505] 1 1 1 1 1 1 1 1 1 1 ... $ : num [1:1505, 1:1505] 1 1 1 1 1 1 1 1 1 1 ... $ : num [1:1505, 1:1505] 1 1 1 1 1 1 1 1 1 1 ... $ : num [1:1505, 1:1505] 1 1 1 1 1 1 1 1 1 1 ... List of 4 $ : num [1:1505, 1:2] -0.0258 -0.0258 -0.0258 -0.0258 -0.0258 ... $ : num [1:1505, 1:2] -0.0258 -0.0258 -0.0258 -0.0258 -0.0258 ... $ : num [1:1505, 1:2] -0.0258 -0.0258 -0.0258 -0.0258 -0.0258 ... $ : num [1:1505, 1:2] -0.0258 -0.0258 -0.0258 -0.0258 -0.0258 ... [1] 6020 2
Expected output: 6020x6020 matrix
R: 3.3.1
OS: Debian 8.5解决方案One option would be to convert to an
array
, butarray
can hold only fixed dimensions. So, if we fell short of number of elements, append some NAs at the end and then convert to a 3D array.m2 <- array(`length<-`(m1, 30), dim = c(2,5,3))
and then
apply
the function by specifying theMARGIN
as 3.res <- apply(m2, 3, FUN = function(x) list(cor(x))) identical(res[[1]][[1]], cor(m2[,,1])) #[1] TRUE
Or another option is to loop through the third dimension using
lapply
and apply thecor
res2 <- lapply(seq(dim(m2)[3]), function(i) cor(m2[,,i]))
data
set.seed(24) m1 <- matrix(rnorm(45), ncol=5, nrow=9)
这篇关于如何调整R中较小的数据结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!