以下是我的數據的矩陣選擇特定行的特定數量,如何簡單地在中的R
num<- c(6,8,7,5,9,6,7)
x<- matrix(c(rep(1:7,num),rnorm(sum(num))), nrow=sum(num), ncol=2)
colnames(x)<-c("subject","value")
X
受試者值
[1,] 1 0.35182560
[2,1] 1 0.35933614
[3,] 1 -0.89029320
[4,] 1 -0.79991981
[5,1] 1 1.10773640
[6,1] 1 -1.73900484
[7,] 2 1.06632139
[8,] 2 0.71727759
[9,] 2 0.51002247
[10,] 2 1.36132224
[11,] 2 -0.85432175
[12,] 2 -0.49878742
[13,] 2 1.43705322
[14,] 2 0.34052593
[15,] 3 -0.43245360
[16,] 3 1.01687525
[17,] 3 0.48998138
[18,] 3 -1.06197379
[19,] 3 -0.19777785
[20,] 3 1.24940714
[21,] 3 0.47521229
[22] 4 -0.99888249
[23] 4 -0.12678874
[24,] 4 -1.14620801
[25] 4 - 1.29165060
[26] 4 1.56110270
[27,] 5 0.82543156
[28,] 5 -0.61718617
[29,] 5 0.22357131
[30,] 5 0.59639380
[31,] 5 2.72122980
[32,] 5 0.58674354
[33,] 5 0.23674196
[34,] 5 0.78656422
[35,] 5 0.10426860
[36,] 6 0.93059568
[37,] 6 0.16065327
[38,] 6 -2.23496916
[39,] 6 -1.75680495
[40,] 6 0.49717967
[41,] 6 1.13033910
[42] 7 0.71402667
[43] 7 -0.06120018
[44,] 7 -0.67636605
[45,] 7 0.46402913
[46,] 7 -0.99090058
[47,] 7 1.58853435
[48,] 7 -1.15982415
我的任務是在每一個主題來選擇數據的特定數目以重新形成新的矩陣。
每個受試者的具體數目是
b<- ceiling(num*0.5)
是
b
[1] 3 4 4 3 5 3 4
也就是說,我需要提取
前3行的主體1
前4行的主體2
前4行su對象3,
...
前4行的主題7,
形成一個新的矩陣。
下面是我自己的編碼:
b<- ceiling(a*0.5)
newx<- matrix(0, nrow=sum(b), ncol=2)
newx<- do.call(rbind, sapply(1:7, function(i){head(x[x[,1]==i,], b[i])}))
它的工作原理,但它需要時間,有沒有更簡單的方法來找出這個問題?
下一頁末
受試者值
[1,] 1 0.35182560
[2,1] 1 0.35933614
[3,] 1 -0.89029320
[4,] 2 1.06632139
[5,1] 2 0.71727759
[6,] 2 0.51002247
[7,] 2 1.36132224
[8,] 3 -0.43245360
[9,] 3 1.01687525
[10,] 3 0 0.48998138
[11,] 3 -1.06197379
[12,] 4 -0.99888249
[13,] 4 -0.12678874
[14,] 4 -1.14620801
[15,] 5 0.82543156
[16 ,] 5 -0.61718617
[17,] 5 0.22357131
[18,] 5 0.59639380
[19,] 5 2.72122980
[20,] 6 0.93059568
[21] 6 0.16065327
[22, ] 6 -2.23496916
[23,] 7 0.71402 667
[24,] 7 -0.06120018
[25,] 7 -0.67636605
[26,] 7 0。46402913
在b的計算中,'a'是什麼? – Gopala
對不起,「a」是num <-c(6,8,7,5,9,6,7),我修改了它,謝謝! – lightsnail
我以爲是。我在下面回答。 – Gopala