我在 R 中有一個 68 列和近 43000 行的矩陣。它基本上是一個巨大的矩陣,由較小的 68*68 矩陣組成。我需要得到每 15 個較小矩陣的平均矩陣(因為每 15 個矩陣等于一個參與者)。所以行 1-68、69-136 等直到 1020 (=15* 68)。我不知道如何制作一個 for 回圈,它每 68 行取一次,并將其與下一個 68 等相加,同時仍然保持 68*68 矩陣。我能夠正確總結它們的唯一方法是對特定行進行索引,但由于我有 43000 行,而這個資料集是 30 多個檔案中的第一個,我不想繼續索引。
誰能幫我找到一種簡單/快速的方法來做到這一點?
編輯:所以資料的一個例子是:
print(Matrix_Alpha_ami[1:3,1:5])
V1 V2 V3 V4 V5
[1,] 0.0000 0.4749 0.5629 0.6339 0.5406
[2,] 0.4749 0.0000 0.3157 0.5234 0.4737
[3,] 0.5629 0.3157 0.0000 0.5707 0.4191
> print(Matrix_Alpha_ami[69:71,1:5])
V1 V2 V3 V4 V5
[69,] 0.0000 0.4993 0.4812 0.5227 0.5018
[70,] 0.4993 0.0000 0.5444 0.6106 0.3324
[71,] 0.4812 0.5444 0.0000 0.5818 0.4107
列一直持續到 V68,行數下降到 42k
資料的第一位是矩陣 1 的開頭,矩陣 2 的第二位。問題是它們不是單個矩陣,而是一個大矩陣的一部分。因此,我不能只說 m1*m2。
最后,我需要一個由 15 個矩陣組成的平均矩陣 - 獲得一個參與者的所有測量值 (n=15) 的平均值。例如,從示例資料中我會得到 ((m1 m2)/2):
V1 V2 V3 V4 V5
[1,] 0.00000 0.48710 0.52205 0.57830 0.52120
[2,] 0.48710 0.00000 0.43005 0.56700 0.40305
[3,] 0.52205 0.43005 0.00000 0.57625 0.41490
uj5u.com熱心網友回復:
我不確定我是否正確理解您要執行的操作,但是我創建了一個向量,其中包含用于子集矩陣行的適當索引
number <- 1020
a <- (seq(1,number,68))
n <- as.numeric(length(a))
b <- vector()
for (i in (1:n)){
b[i] <- a[i 1]-1}
b[n] <- number
c <- paste(a,b, sep = ":")
c
[1] "1:68" "69:136" "137:204" "205:272" "273:340"
[6] "341:408" "409:476" "477:544" "545:612" "613:680"
[11] "681:748" "749:816" "817:884" "885:952" "953:1020"
uj5u.com熱心網友回復:
如果我正確理解了這個問題,以下函式可能會回答它。
該函式通過與參與者對應的子矩陣分割輸入矩陣,然后通過每個參與者內的子矩陣計算列均值。
回傳值是一個平均值矩陣串列。
funMean <- function(x, rows, matrices){
f <- c(1, rep(0, rows*matrices - 1L))
f <- rep(f, length.out = nrow(x))
f <- cumsum(f)
#
g <- c(1, rep(0, rows - 1L))
g <- rep(g, matrices)
g <- cumsum(g)
#
x <- split(x, f)
x <- lapply(x, matrix, ncol = rows)
y <- lapply(x, \(X){
z <- split(X, g)
z <- lapply(z, matrix, ncol = rows)
t(sapply(z, rowMeans, na.rm = TRUE))
})
y
}
對于假資料,不是 68*68 Rows*Rows,Mats而是 15。
Rows <- 5
Mats <- 3
y <- funMean(Matrix_Alpha_ami, Rows, Mats)
y[[1]]
# [,1] [,2] [,3] [,4] [,5]
#1 -0.9407742 0.40359467 0.233171598 -0.004998849 0.54604432
#2 -1.1864782 -0.58013231 0.004050014 0.102806769 -0.28126799
#3 -0.2209807 0.08324887 -0.417034539 0.695183587 0.02515484
do.call(rbind, y)
測驗資料
set.seed(2021)
Matrix_Alpha_ami <- lapply(1:10, \(k){
matrix(rnorm(Mats*Rows^2), ncol = Rows)
})
Matrix_Alpha_ami <- do.call(rbind, Matrix_Alpha_ami)
dim(Matrix_Alpha_ami)
uj5u.com熱心網友回復:
此解決方案使用一系列索引來構建一個 15 行的矩陣以傳遞給colMeans。它應該運行得非常快。
d <- 68L
s <- 15L
# traceable example matrix--replace m with your matrix
m <- matrix(1:d, nrow = d*s*10, ncol = d)
m <- m (col(m) - 1)*d ((row(m) - 1) %/% (d*s))*d^2
# solution
i1 <- seq(1L, by = d*s, length.out = nrow(m)/d/s) # top left index of the first d-by-d matrix for each individual
i2 <- sequence(rep(d, length(i1)), i1) # indices of left-most column of the first d-by-d matrix for each individual
i3 <- sequence(rep(d, length(i2)), i2, nrow(m)) # indices of the first d-by-d matrix for each individual
i4 <- sequence(rep(s, length(i3)), i3, d) # indices for averaging (by sets of s)
m2 <- matrix(colMeans(matrix(m[i4], nrow = s)), ncol = d, byrow = TRUE)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/379170.html
上一篇:限制惰性列中的專案
