本文介绍了如何调整R中较小的数据结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我在以下数据结构中有四条完整的信号。我想将每个信号分成360个块或靠近它。 当前数据结构 [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 #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) m1 / pre> 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 because 1:4*1504 would lose some tail points>>> 541650*4.02166600.0>>> 360*1505*42167200.0Current data structure, current code and its contents in Rm1 <- 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 matrix Testing akrun's answer with the case exampleCode # http://stackoverflow.com/q/40429343/54964library("corrgram")set.seed(24)A=541650m1 <- matrix(1:A, ncol=4, nrow=A)a=360; b=1505; c=4;# http://stackoverflow.com/a/40430229/54964m2 <- 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 matrixdim(res2) # 6020 2# Not Passed because output strangecorrgram(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 matrixList 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 2Expected output: 6020x6020 matrix R: 3.3.1OS: Debian 8.5 解决方案 One option would be to convert to an array, but array 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 the MARGIN as 3.res <- apply(m2, 3, FUN = function(x) list(cor(x)))identical(res[[1]][[1]], cor(m2[,,1]))#[1] TRUEOr another option is to loop through the third dimension using lapply and apply the corres2 <- lapply(seq(dim(m2)[3]), function(i) cor(m2[,,i]))dataset.seed(24)m1 <- matrix(rnorm(45), ncol=5, nrow=9) 这篇关于如何调整R中较小的数据结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-11 03:09