在单个条件下使用data.table替换多列 [英] using data.table to replace multiple columns on single condition

查看:52
本文介绍了在单个条件下使用data.table替换多列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将默认值(255)更改为NA.

I want to change the default value (which is 255) to NA.

dt <- data.table(x = c(1,5,255,0,NA), y = c(1,7,255,0,0), z = c(4,2,7,8,255))
coords <- c('x', 'y')

其中提供以下代码:

     x   y   z
1:   1   1   4
2:   5   7   2
3: 255 255   7
4:   0   0   8
5:  NA   0 255

我想出的最远的是这个:

I the furthest I came up with is this:

dt[.SD == 255, (.SD) := NA, .SDcols = coords]

请注意,z列保持不变.因此,仅指定的列而不是所有的列.

Please note that column z stays the same. So just the columns which are specified and not all columns.

但这并不能帮助我获得解决方案:

But that doesn't help me to get the sollution:

     x   y   z
1:   1   1   4
2:   5   7   2
3:  NA  NA   7
4:   0   0   8
5:  NA   0 255

我正在寻找一种可持续的解决方案,因为原始数据集有几百万行.

I am looking for a sustainable solution because the original dataset is a couple of million rows.

我找到了一个解决方案,但是它相当丑陋,而且肯定太慢了,因为它要花费近10秒钟才能通过22009 x 86的数据帧.有人能找到更好的解决方案吗?

I have found a solution but it is quite ugly and is definately too slow as it takes almost 10 seconds to get through a dataframe of 22009 x 86. Does anyone have a better solution?

代码:

dt [,replace(.SD,.SD == 255,NA),.SDcols =坐标,by = c(colnames(dt)[!colnames(dt)%in%coords])]

推荐答案

在这里,您可以将列保留在 .SDcols

Here is how you can keep the columns outside .SDcols,

library(data.table)
dt[, (coords) := replace(.SD, .SD == 255, NA), .SDcols = coords]

给出,

    x  y   z
1:  1  1   4
2:  5  7   2
3: NA NA   7
4:  0  0   8
5: NA  0 255

这篇关于在单个条件下使用data.table替换多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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