[R] iterators : checkFunc with ireadLines
Laurent Rhelp
L@urentRHe|p @end|ng |rom |ree@|r
Sun May 17 15:52:30 CEST 2020
Dear R-Help List,
I would like to use an iterator to read a file filtering some
selected lines according to the line name in order to use after a
foreach loop. I wanted to use the checkFunc argument as the following
example found on internet to select only prime numbers :
| iprime <- ||iter||(1:100, checkFunc =
||function||(n) ||isprime||(n))|
|(https://datawookie.netlify.app/blog/2013/11/iterators-in-r/)
<https://datawookie.netlify.app/blog/2013/11/iterators-in-r/>|
but the checkFunc argument seems not to be available with the function
ireadLines (package iterators). So, I did the code below to solve my
problem but I am sure that I miss something to use iterators with files.
Since I found nothing on the web about ireadLines and the checkFunc
argument, could somebody help me to understand how we have to use
iterator (and foreach loop) on files keeping only selected lines ?
Thank you very much
Laurent
Presently here is my code:
## mock file to read: test.txt
##
# Time 0 0.000999 0.001999 0.002998 0.003998 0.004997
0.005997 0.006996 0.007996
# N023 -0.031323 -0.035026 -0.029759 -0.024886 -0.024464
-0.026816 -0.03369 -0.041067 -0.038747
# N053 -0.014083 -0.004741 0.001443 -0.010152 -0.012996
-0.005337 -0.008738 -0.015094 -0.012104
# N123 -0.019008 -0.013494 -0.01318 -0.029208 -0.032748
-0.020243 -0.015089 -0.014439 -0.011681
# N163 -0.054023 -0.049345 -0.037158 -0.04112 -0.044612
-0.036953 -0.036061 -0.044516 -0.046436
# N193 -0.022171 -0.022384 -0.022338 -0.023304 -0.022569
-0.021827 -0.021996 -0.021755 -0.021846
# sensors to keep
sensors <- c("N053", "N163")
library(iterators)
library(rlist)
file_name <- "test.txt"
con_obj <- file( file_name , "r")
ifile <- ireadLines( con_obj , n = 1 )
## I do not do a loop for the example
res <- list()
r <- get_Lines_iter( ifile , sensors)
res <- list.append( res , r )
res
r <- get_Lines_iter( ifile , sensors)
res <- list.append( res , r )
res
r <- get_Lines_iter( ifile , sensors)
do.call("cbind",res)
## the function get_Lines_iter to select and process the line
get_Lines_iter <- function( iter , sensors, sep = '\t', quiet = FALSE){
## read the next record in the iterator
r = try( nextElem(iter) )
while( TRUE ){
if( class(r) == "try-error") {
return( stop("The iterator is empty") )
} else {
## split the read line according to the separator
r_txt <- textConnection(r)
fields <- scan(file = r_txt, what = "character", sep = sep, quiet =
quiet)
## test if we have to keep the line
if( fields[1] %in% sensors){
## data processing for the selected line (for the example
transformation in dataframe)
n <- length(fields)
x <- data.frame( as.numeric(fields[2:n]) )
names(x) <- fields[1]
## We return the values
print(paste0("sensor ",fields[1]," ok"))
return( x )
}else{
print(paste0("Sensor ", fields[1] ," not selected"))
r = try(nextElem(iter) )}
}
}# end while loop
}
--
L'absence de virus dans ce courrier électronique a été vérifiée par le logiciel antivirus Avast.
https://www.avast.com/antivirus
[[alternative HTML version deleted]]
More information about the R-help
mailing list