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),]
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.