如何将矢量列表拆分成列或矩阵?

时间:2017-02-26 00:52:14

标签: r regex data-conversion mapply

我有一个采用这种形式的载体列表:

package test;

import java.util.ArrayList;

public class Theatre {
   //Number of rows in the theatre
   public static final int NUMBER_ROWS = 10;
   //Number of seats that are in each row
   public static final int NUMBER_OF_SEATS_IN_ROW = 15;
   ArrayList<ArrayList<Integer>> seat = new ArrayList<ArrayList<Integer>>();

   public Theatre(){
        for(int x=0; x<NUMBER_ROWS;x++){
            ArrayList<Integer> arrSeat = new ArrayList<Integer>();
            for(int y=0;y<NUMBER_OF_SEATS_IN_ROW; y++) {
                if(x<5){ // If row is less than 5, set price of seat to 100
                    arrSeat.add(100);
                }else{ // If row is not less than 5, set price to 70
                    arrSeat.add(70);
                }
            }
            seat.add(arrSeat);
        }
    }

    /**
     * This method displays all the seats and gives a visual representation     of which seats are reserved
     */
    public void showReservations(){
         String output = "";
         for(int x=0;x<NUMBER_ROWS;x++){
             for(int y=0;y<NUMBER_OF_SEATS_IN_ROW;y++){
                 if(x==2 && y==3 || x==5&&y==2 || x==4&&y==4) {
                     seat.get(x).set(y, 0);
                 }
                 if(seat.get(x).get(y) == 0) {
                     output += " x ";
                 } else {
                     output += " o ";
                 }
             }
             output += "Row "+(x+1)+"\n"; // Append newline character when row is complete
          }
          System.out.println(output);
      }

      public static void main(String[] args) {
          Theatre theatre = new Theatre();
          theatre.showReservations();
     }
}

我的目标是在此结构上使用> g [[1]] [1] "L" "14" "L" "39" "L" "61" "B" "0" "L" "15" "L" "59" "W" "64" [[2]] [1] "L" "62" "D" "31" "L" "10" "L" "30" "B" "0" "D" "45" "L" "43" [[3]] [1] "H" "0" "L" "11" "L" "35" "W" "45" "H" "0" "L" "40" "L" "42" 并将14列中的每一列转换为向量。第一列是:

mapply

第二栏是:

[[1]]
[1] "L"

[[2]]
[1] "L"

[[3]]
[1] "H"

等等。我怀疑结构是一个矩阵(?),但我不确定。我使用了很多[[1]] [1] "14" [[2]] [1] "62" [[3]] [1] "0" lapply的{​​{1}}和正则表达式来获取这一点,但我不确定如何继续。我怀疑该函数会使用类似stringr的模式作为文本和str_extract_all,我知道"[A-Z]{1}"可以返回矩阵,但我不知道从哪里开始。

2 个答案:

答案 0 :(得分:1)

这是使用mapply的解决方案

g <- list()
g[[1]] <- c("L",  "14", "L",  "39", "L",  "61", "B",  "0",  "L",  "15", "L",  "59", "W",  "64")
g[[2]] <- c("L",  "62", "D",  "31", "L",  "10", "L",  "30", "B",  "0",  "D",  "45", "L",  "43")
g[[3]] <- c("H",  "0",  "L",  "11", "L",  "35", "W",  "45", "H",  "0",  "L",  "40", "L",  "42")

考虑一下你要在每个列表元素中提取第一个元素的简单情况,你可以使用lapply:

lapply(g, function (x) x[1])

现在我们可以使用mapply迭代:

lengths(g) # returns length of each element in the list
g2 <- mapply(function(y) lapply(g, function (x) x[y]), 1:lengths(g)[1])
g2

#      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
# [1,] "L"  "14" "L"  "39" "L"  "61" "B"  "0"  "L"  "15"  "L"   "59"  "W"   "64" 
# [2,] "L"  "62" "D"  "31" "L"  "10" "L"  "30" "B"  "0"   "D"   "45"  "L"   "43" 
# [3,] "H"  "0"  "L"  "11" "L"  "35" "W"  "45" "H"  "0"   "L"   "40"  "L"   "42" 

g2[,1]
# [[1]]
# [1] "L"

# [[2]]
# [1] "L"

# [[3]]
# [1] "H"

unlist(g2[,1])
# [1] "L" "L" "H"

答案 1 :(得分:1)

作为do.call(rbind, g)的替代,我们可以使用data.frame实际上是所有向量具有相同长度的向量列表这一事实。因此,给定的结构g可以转换为data.frame,然后根据请求转换为矩阵。

重现数据:

g <- list(
  c("L",  "14", "L",  "39", "L",  "61", "B",  "0",  "L",  "15", "L",  "59", "W",  "64"),
  c("L",  "62", "D",  "31", "L",  "10", "L",  "30", "B",  "0",  "D",  "45", "L",  "43"),
  c("H",  "0",  "L",  "11", "L",  "35", "W",  "45", "H",  "0",  "L",  "40", "L",  "42")
)

<强>变换:

m <- t(as.data.frame(g))
dimnames(m) <- NULL   # remove deafault row names
m
#     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
#[1,] "L"  "14" "L"  "39" "L"  "61" "B"  "0"  "L"  "15"  "L"   "59"  "W"   "64" 
#[2,] "L"  "62" "D"  "31" "L"  "10" "L"  "30" "B"  "0"   "D"   "45"  "L"   "43" 
#[3,] "H"  "0"  "L"  "11" "L"  "35" "W"  "45" "H"  "0"   "L"   "40"  "L"   "42"

访问列:

m[, 1]
#[1] "L" "L" "H"