[Rd] [External] Re: New pipe operator
Gabor Grothendieck
ggrothend|eck @end|ng |rom gm@||@com
Mon Dec 7 15:09:41 CET 2020
On Sat, Dec 5, 2020 at 1:19 PM <luke-tierney using uiowa.edu> wrote:
> Let's get some experience
Here is my last SO post using dplyr rewritten to use R 4.1 devel. Seems
not too bad. Was able to work around the placeholder for gsub by specifying
the arg names and used \(...)... elsewhere. This does not address the
inconsistency discussed though. I have indented by 2 spaced in case the
email wraps around. The objective is to read myfile.csv including columns that
contain c(...) and integer(0), parsing and evaluating them.
# taken from:
# https://stackoverflow.com/questions/65174764/reading-in-a-csv-that-contains-vectors-cx-y-in-r/65175172#65175172
# create input file for testing
Lines <- "\"col1\",\"col2\",\"col3\"\n\"a\",1,integer(0)\n\"c\",c(3,4),5\n\"e\",6,7\n"
cat(Lines, file = "myfile.csv")
#########################################################################
# base R 4.1 (devel)
DF <- "myfile.csv" |>
readLines() |>
gsub(pattern = r'{(c\(.*?\)|integer\(0\))}', replacement = r'{"\1"}') |>
\(.) read.csv(text = .) |>
\(.) replace(., 2:3, lapply(.[2:3], \(col) lapply(col, \(x)
eval(parse(text = x)))))
#########################################################################
# dplyr/magrittr
library(dplyr)
DF <- "myfile.csv" %>%
readLines %>%
gsub(r'{(c\(.*?\)|integer\(0\))}', r'{"\1"}', .) %>%
{ read.csv(text = .) } %>%
mutate(across(2:3, ~ lapply(., function(x) eval(parse(text = x)))))
More information about the R-devel
mailing list