I have a large data.table in R and would like to set any negative numbers in a subset of columns to zero. I have looked at multiple options for doing this but I can't find answer I need.
Hi Riffomonas, thanks for getting back to me. This does work - I was missing the "v2" part, so this is a step forward. However, I would ideally like to do this in a single step, either using the named columns or column indices as this is within a loop. I am also using the R data.table package, so it is a data.table as well as a data.frame, but data.frame options do still work for the most part.
p.s. while data.table is a common library but as demonstratedd on this thread, not ubiquitous. Therefore it would be good to add library(data.table) to your example ( in the future).
HI Nir, thanks for your response. I'll cite the package next time. Unfortunately the code you have supplied doesn't work as it filters only on the negative values in 'v2' and turns the values on those rows in 'v2 and 'v3' to 0. I would like to search for negative values in v2 and v3 and only turn the negative values in those columns to 0, rather than across the row.
sorry about that. data.table is its own syntax that one would have to study and learn.
I will skip that by applying my dplyr knowledge and using dtplyr package to run data.table queries.
Thanks for this link, it helped me to see a way through (though if anyone has a better way to do this I'd love to see it). In the end I wrote a neg2zero function and applied that.