Reading specific rows of large matrix data file

前端 未结 4 1009
粉色の甜心
粉色の甜心 2021-02-09 22:50

Suppose I have a gigantic m*n matrix X (that is too big to read into memory) and binary numeric vector V with length m. My objective is to

4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-09 22:54

    I think you can use sqldf package to achieve what you want. sqldf reads the csv file directly into SQLlite database bypassing R environment altogether.

    library(sqldf)
    
    Xfile <- file('target.csv')
    sqlcmd <- paste0('select * from Xfile where rowid in (', paste(which(V==1), collapse=','), ')')
    sqldf(sqlcmd, file.format=list(header=TRUE))
    

    or:

    library(sqldf)
    
    Vdf <- data.frame(V)
    sqlcmd <- "select file.* from file, Vdf on file.rowid = Vdf.rowid and V = 1"
    read.csv.sql("target.csv", sql = sqlcmd)
    

提交回复
热议问题