更改为data.table子集的随机选择的值 [英] Change values for a random selection of a data.table subset

查看:93
本文介绍了更改为data.table子集的随机选择的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上是此扩充功能问题,因为我注意到,如果你第二次子集化,不可能更改列的值。

Basically an extension to this question, because I noticed, that if you are subsetting for a second time it's not possible to change a value of a column.

random.length  <-  sample(x = 15:30, size = 1)
dt <- data.table(city=sample(c("Cape Town", "New York", "Pittsburgh", "Tel Aviv", "Amsterdam"), size=random.length, replace = TRUE), score = sample(x=1:10, size = random.length, replace=TRUE)) 
set.seed(1)
dt[sample(.N,3), score :=9999]
set.seed(1)
dt[sample(.N,3),]

这样可以正常工作,并将分数改为9999城市。虽然如果你在第一步中子集,然后做抽样,并尝试分配一个新的分数值是不可能的。

This works as expected and changes the score to 9999 for the three randomly selected cities. Although if you subset in a first step and then do the sampling and try to assign a new score value it's not possible.

set.seed(1)
dt[city == "New York",][sample(.N,1), score := 55555]
set.seed(1)
dt[city == "New York",][sample(.N,1)]

喜欢实现的是我可以改变一些列的值,这是某个子集的一部分,并从这个子集随机选择。

What I would like to achieve is that I can change a value of some column, which is part of certain subset and gets randomly selected from this subset.

推荐答案

除了上述所有建议之外,您还可以对索引进行取样(可以使用其中函数计算):

You can also sample the index ( which can be calculated using which function ) besides all the suggestions above:

dt[sample(which(city == "New York"), 1), score:=555L]
dt
#           city score
#  1:   Tel Aviv     8
#  2:  Amsterdam     3
#  3:  Cape Town    10
#  4:   New York     1
#  5:  Cape Town    10
#  6: Pittsburgh     2
#  7: Pittsburgh     8
#  8:  Amsterdam    10
#  9:  Amsterdam     8
# 10:  Amsterdam     4
# 11:   Tel Aviv     7
# 12:  Amsterdam     2
# 13: Pittsburgh     1
# 14:  Amsterdam     3
# 15: Pittsburgh     2
# 16:   New York     7
# 17:   Tel Aviv    10
# 18:   New York    10
# 19:  Cape Town     1
# 20:  Amsterdam     7
# 21:  Amsterdam     3
# 22:   New York   555
# 23:  Cape Town     6
# 24:   New York     1
# 25:   Tel Aviv    10
#           city score

这篇关于更改为data.table子集的随机选择的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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