如何在data.table中按名称删除列? - How do you delete a column by name in data.table?

10/14 10:45
阅读数 0

问题:

To get rid of a column named "foo" in a data.frame , I can do:要摆脱data.frame名为“foo”的data.frame ,我可以这样做:

df <- df[-grep('foo', colnames(df))]

However, once df is converted to a data.table object, there is no way to just remove a column.但是,一旦将df转换为data.table对象,就无法仅删除列。

Example:例子:

df <- data.frame(id = 1:100, foo = rnorm(100))
df2 <- df[-grep('foo', colnames(df))] # works
df3 <- data.table(df)
df3[-grep('foo', colnames(df3))] 

But once it is converted to a data.table object, this no longer works.但是一旦它被转换为data.table对象,这将不再有效。


解决方案:

参考一: https://en.stackoom.com/question/cby1
参考二: https://stackoom.com/question/cby1
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部