R data.table-带有':='的新列并保留现有列 [英] R data.table - new column with ':=' and keep existing column

查看:54
本文介绍了R data.table-带有':='的新列并保留现有列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以创建一个新列,并在语句中保留(很少)现有列?例如创建"x"列,然后保留"x"和"mpg"列

Is it possible to create a new column and keep (few) existing columns in the statement ? e.g. creation of "x" column and then keeping "x" and "mpg" column

dt <- data.table(mtcars)

dt[,x:=mpg]

dt[,.(x,mpg)]

推荐答案

如果您要使用:= 进行引用替换,则可以

If you want to do the replacement by reference, using := then you can do

dt[, x:=mpg][, setdiff(colnames(dt), c('x', 'mpg')) := NULL]

这篇关于R data.table-带有':='的新列并保留现有列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆