[R] iterators : checkFunc with ireadLines

Ivan Krylov kry|ov@r00t @end|ng |rom gm@||@com
Wed May 20 09:46:14 CEST 2020


Hi Laurent,

I am not saying this will work every time and I do recognise that this
is very different from a more general solution that you had envisioned,
but if you are on an UNIX-like system or have the relevant utilities
installed and on the %PATH% on Windows, you can filter the input file
line-by-line using a pipe and an external program:

On Sun, 17 May 2020 15:52:30 +0200
Laurent Rhelp <LaurentRHelp using free.fr> wrote:

> # sensors to keep
> sensors <-  c("N053", "N163")

# filter on the beginning of the line
i <- pipe("grep -E '^(N053|N163)' test.txt")
# or:
# filter on the beginning of the given column
# (use $2 for the second column, etc.)
i <- pipe("awk '($1 ~ \"^(N053|N163)\")' test.txt")
# or:
# since your message is full of Unicode non-breaking spaces, I have to
# bring in heavier machinery to handle those correctly;
# only this solution manages to match full column values
# (here you can also use $F[1] for second column and so on)
i <- pipe("perl -CSD -F'\\s+' -lE \\
 'print join qq{\\t}, @F if $F[0] =~ /^(N053|N163)$/' \\
 test.txt
")
lines <- read.table(i) # closes i when done

The downside of this approach is having to shell-escape the command
lines, which can become complicated, and choosing between use of regular
expressions and more wordy programs (Unicode whitespace in the input
doesn't help, either).

-- 
Best regards,
Ivan



More information about the R-help mailing list