【发布时间】:2018-02-18 00:32:00
【问题描述】:
我正在编写一个重写列名的函数,以便在标准中输出 data.table。输入是用户提供的 data.tables,可能有几个名称不同。
这是所有输入 data.tables 的输出格式:
length width height weight
输入的 data.tables 可能看起来像,例如
input_dt = data.table(
length = 194,
wide = 36,
tall = 340,
kilogram = 231.2
)
我的函数会将此 data.table(或 data.frame)作为输入,并更改列,输出此 data.table:
length width height weight
194 36 340 231.2
我为检查可能名称的函数创建了一个key:
key = list(
length = c('long'),
width = c('girth', 'WIDTH', 'wide'),
height = c('tall', 'high'),
weight = c('Weight', 'WEIGHT', 'kilogram', 'pound', 'kilograms', 'pounds')
)
现在,在函数内,我可以通过检查交集来检查input_dt的输入列名称是否需要更改:
> intersect(names(input_dt), unlist(key))
[1] "wide" "tall" "kilogram"
然后适当地改变这些。我的问题是:
编写这个自定义函数会充满 for 循环,而且效率很低。给定自定义的值“键”,是否还有其他对 data.table 更友好的解决方案可用?
【问题讨论】:
标签: r data.table multiple-columns