2014-05-10 52 views
0

樣本數據:反向順序排在列表中,其中R

list.data <- list(matrix(seq(1,30,2),5,3),matrix(seq(1,10,2),5,2)) 

NumOnly <- lapply(list.data, function(x) x[,2:ncol(x), drop = FALSE]) 

轉置矩陣

TranData <- lapply(NumOnly, function(x) t(x)) 

反向/翻轉順序

DataModel <- lapply(TranData, function(x) ifelse(nrow(x)==1,x,apply(x, 2, rev))) 
head(DataModel) 

如何扭轉行順序爲矩陣row>1並以row==1開始。

以上DataModel輸出不知何故只給出了第一項...

我想獲得:

[[1]] 
    [,1] [,2] [,3] [,4] [,5] 
[1,] 21 23 25 27 29 
[2,] 11 13 15 17 19 

    [[2]] 
    [,1] [,2] [,3] [,4] [,5] 
[1,] 1 3 5 7 9 

回答

3

我會內[使用rev,像這樣:

> lapply(TranData, function(x) x[rev(sequence(nrow(x))), , drop = FALSE]) 
[[1]] 
    [,1] [,2] [,3] [,4] [,5] 
[1,] 21 23 25 27 29 
[2,] 11 13 15 17 19 

[[2]] 
    [,1] [,2] [,3] [,4] [,5] 
[1,] 1 3 5 7 9 
+0

完美, 謝謝! – Maximilian