機略戦記

Maneuver warfare

Rで、指定したdata.tableの全てのカラム名にprefixを付けたい。

結論

table <- data.table(a=1,b=2,c=3)
prefix <- 'hoge'

old_names <- names(table)
new_names <- paste(prefix, old_names, sep='.')

setnames(table, old_names, new_names)

これで、 処理前にnames(table) #=> "a" "b" "c"だったカラム名が、 names(table) #=> "hoge.a" "hoge.b" "hoge.c"になる。

感想

ループもイテレーターも使わずに色々できるの楽しい。