fast subsetting in R

后端 未结 5 499
情书的邮戳
情书的邮戳 2021-02-03 14:00

I\'ve got a dataframe dat of size 30000 x 50. I also have a separate list that contains points to groupings of rows from this dataframe, e.g.,

rows <- list(c(         


        
5条回答
  •  时光取名叫无心
    2021-02-03 14:40

    I agree with mathematical coffee that I too get fast times for this.

    Don't know if it's possible but by unlisting as a vector and then converting to numeric you can get a speed boost.

    dat <- data.frame(matrix(rnorm(30000*50), 30000, 50 ))
    rows <- as.numeric(unlist(list(c("34", "36", "39"), c("45", "46"))))
    system.time(lapply(rows, function(r) {dat[r, ]}))
    

    EDIT:

    dat$observ <- rownames(dat)
    rownames(dat) <- 1:nrow(dat)
    

提交回复
热议问题