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

守給你的承諾、 提交于 2019-12-01 01:38:43

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/41228076/using-data-tables-shortcut-in-quoted-expressions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!