Skip to content

Instantly share code, notes, and snippets.

@abikoushi
Created October 31, 2024 02:25
Show Gist options
  • Save abikoushi/90c8b06c0f3c2be38f4fa765cb5544f8 to your computer and use it in GitHub Desktop.
Save abikoushi/90c8b06c0f3c2be38f4fa765cb5544f8 to your computer and use it in GitHub Desktop.
read specific rows on text file
scan1_mtx <- function(con, skip = 0){
base::scan(con, nmax = 1, quiet=TRUE,
what = list(i=integer(), j=integer(), v=numeric()),
skip = skip)
}
dataloader_mtx2 <- function(file_path, bag){
con <- file(file_path, open = "r") #Open for reading in text mode
#get matrix size
rowsize <- scan(con, what=integer(), comment.char = "%", nmax=1, quiet = TRUE)
colsize <- scan(con, what=integer(), nmax=1, quiet = TRUE)
len <- scan(con, what=integer(), nmax=1, quiet = TRUE)
bag <- sort(bag)
newL <- scan1_mtx(con, skip = bag[1] - 1L) #initialize
out <- matrix(0, length(bag), 3)
out[1,] <- unlist(newL)
bag <- diff(bag)
for(i in 1:length(bag)){
newL <- scan1_mtx(con, skip = bag[i] - 1L)
out[i+1,] <- unlist(newL)
}
close(con)
return(out)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment