subsetting matrix with id from another matrix

前端 未结 4 1133
闹比i
闹比i 2021-01-24 09:43

I would like to subset the data of one matrix using data in a second matrix. The columns of one matrix is labeled. For example,

area1 <- c(9836374,635440,230         


        
相关标签:
4条回答
  • 2021-01-24 10:04

    I think this covers it and fits with your description:

    spl <- cumsum(apply(mat2,1, function(x) all(x==c(1,2,5))))
    split(as.data.frame(mat1),spl)
    
    #$`1`
    #       a1     a2      a3
    #1 9836374 635440   23018
    #2  833696 936079 1472449
    # 
    #$`2`
    #      a1     a2     a3
    #3 879042 220539 870581
    #
    #$`3`
    #      a1     a2     a3
    #4 217418 552303 269359
    #
    #$`4`
    #      a1     a2      a3
    #5 833696 936079 1472449
    #6 879042 220539  870581
    #7 833696 936079 1472449
    #8 879042 220539  870581
    

    The result fits with "submatrix 1 would have 2 rows of data, submatrix 2 and 3 have 1 row each, and submatrix 4 would have 4 rows of data from mat1"

    0 讨论(0)
  • 2021-01-24 10:08

    I think from what you said, you wanted to keep it as a data frame. You can easily make submatrices by grabbing rows with certain column values.

    Here, I put the data frame back together and made a submatrix just for 1. You can easily add onto it by doing something like using cbind on multiple "area1" columns.

    > area1 <- c(9836374,635440,23018,833696,936079,1472449,879042,220539,870581,217418,552303,269359,833696,936079,1472449,879042,220539,870581, 833696,936079,1472449,879042,220539,870581)
    > id <- c(1,2,5,30,31,34,1,2,5,1,2,5,1,2,5,30,31,34,51,52,55,81,82,85)
    > original<-as.data.frame(cbind(id,area1))
    > original[original$id==1,]
       id   area1
    1   1 9836374
    7   1  879042
    10  1  217418
    13  1  833696
    

    Then you can do what I said before like this.

    > col1<-original[original$id==1,"area1"]
    > col2<-original[original$id==2,"area1"]
    > col3<-original[original$id==5,"area1"]
    > submat<-cbind(col1,col2,col3)
    > colnames(submat)<-c("a1","a2","a3")
    > submat
              a1     a2      a3
    [1,] 9836374 635440   23018
    [2,]  879042 220539  870581
    [3,]  217418 552303  269359
    [4,]  833696 936079 1472449
    
    0 讨论(0)
  • 2021-01-24 10:24
    mat1[which(mat2[,1]==1 & mat2[,2]==2 & mat2[,3]==5),]
            [,1]   [,2]    [,3]
    [1,] 9836374 635440   23018
    [2,]  879042 220539  870581
    [3,]  217418 552303  269359
    [4,]  833696 936079 1472449
    
    0 讨论(0)
  • 2021-01-24 10:27
    split(as.data.frame(mat1), apply(mat2, 1, paste, collapse = " "))
    #$`1 2 5`
    #       a1     a2      a3
    #1 9836374 635440   23018
    #3  879042 220539  870581
    #4  217418 552303  269359
    #5  833696 936079 1472449
    #
    #$`30 31 34`
    #      a1     a2      a3
    #2 833696 936079 1472449
    #6 879042 220539  870581
    #
    #$`51 52 55`
    #      a1     a2      a3
    #7 833696 936079 1472449
    #
    #$`81 82 85`
    #      a1     a2     a3
    #8 879042 220539 870581
    
    0 讨论(0)
提交回复
热议问题