0

Probably easy question (hopefully not duplicate, no idea how to phrase it) - how to update multiple questions in R at the same time with various outputs if they would alter the initial filter?

An abstract example:

id_code <- sample(1:1000,20)
age <- sample(40:80,20, replace=T)
heart_disease <- sample(0:1,20,replace=T)
weight <- sample(105:250,20,replace=T)
dat <- data.frame(id_code,age,heart_disease,weight) 
dat <- dat[dat$age > 50 & dat$heart_disease ==1, ] #???

Looking at this case I would like to change in this example let's say age to 50 and heart_disease to 0 (but only the records that age is > 50 and hd = 1). If I do it iteratively, after the first operation the filter will no longer yield the same records - how can I update it in one go?

The expected output is how can I transform dat that is filtered if the operation would affect filter and requires 2 steps (as explained in the paragraph above)

Slav
  • 469
  • 4
  • 18
  • 1
    Not clear about the expected output – akrun Mar 15 '17 at 14:55
  • You can't update a subset of multiple columns in "one go" with base R. You might be able to do with the the `data.table` library. But probably better just to break it up into two steps. Also note that a good [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) should specific a `set.seed()` when using `sample()` so it's reproducible. And you should give the desired output as a data.frame so we can test possible solutions to see they give the exact results you expect for the sample input. – MrFlick Mar 15 '17 at 15:12
  • you cannot break it into 2 steps as every step would invalidate the filter - if you set hd to 1 then the next operation (setting age) would apply to both records that initially had 1 or received 1 in the first step. That's the problem - so it cannot be done iteratively – Slav Mar 15 '17 at 15:17
  • 1
    @Slav No. You first generate the list of indexes for the rows you want to change, then you use that list of indexes to change the values. You don't recalculate the subset. So not iteratively , but sequentially. – MrFlick Mar 15 '17 at 15:29
  • Thank you, it seems it is less fiddly if I just use data.table library. hoped there was some hidden beauty in the base that would allow the operation – Slav Mar 15 '17 at 15:31

0 Answers0