Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using data.table's .() shortcut in quoted expressions

Tags:

r

data.table

eval

I have some data.tables containing file names as a var named fn. I want to split off basename and extension:

library(data.table)
library(tools)

DT1 = data.table(fn = c("gah.csv", "egad.csv"))
DT2 = data.table(fn = c("gah.xlsx", "egad.xlsx"))
DT3 = data.table(fn = c("boo.txt", "ya.foo"))

do_split_fn = quote(c("name", "ext") := list(file_path_sans_ext(fn), file_ext(fn)))

DT1[, eval(do_split_fn)]
DT2[, eval(do_split_fn)]
DT3[, eval(do_split_fn)]

So this all works fine and my question is very minor: Can I use an expression more like this?

do_split_fn_dot = quote(c("name", "ext") := .(file_path_sans_ext(fn), file_ext(fn)))
DT1[, eval(do_split_fn_dot)]
# Error in eval(expr, envir, enclos) : could not find function "."

That is, I'm trying to swap list() for .(), as one can do inside `[.data.table`.

My quote/eval stuff is an attempt at following recommendations in the data.table FAQ 1.6.

like image 833
Frank Avatar asked Dec 19 '16 17:12

Frank


1 Answers

Already fixed

library(data.table)
library(tools)

DT1 = data.table(fn = c("gah.csv", "egad.csv"))

do_split_fn_dot = quote(c("name", "ext") := .(file_path_sans_ext(fn), file_ext(fn)))
DT1[, eval(do_split_fn_dot)]
DT1
#         fn   name    ext
#1:  gah.csv    gah    csv
#2: egad.csv   egad    csv
like image 101
jangorecki Avatar answered Oct 18 '22 19:10

jangorecki