Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replacing values in data.table by array indexing #2098

Open
demuellae opened this issue Apr 4, 2017 · 3 comments
Open

Replacing values in data.table by array indexing #2098

demuellae opened this issue Apr 4, 2017 · 3 comments

Comments

@demuellae
Copy link

demuellae commented Apr 4, 2017

Hi,
I am still new to this great package. So sorry, if this has been answered or addressed before.
I have the following problem:
Let's say, I have a data.table, e.g.

dt <- data.table(letters=letters, random1=rnorm(26), index=1:26)

now, I would like to replace values at specific indices, given in a separate, logical-valued matrix of equal dimension by NA.

maskMatrix <- matrix(sample(c(TRUE,FALSE), nrow(dt)*ncol(dt), replace=TRUE), nrow=nrow(dt), ncol=ncol(dt))

using data.frames I used to be able to do this:

dt[maskMatrix] <- NA

What would be a correct and efficient way to do this with data.table objects?

Thanks for your help!

Best,
demuellae

@demuellae
Copy link
Author

To clarify: dt[maskMatrix] <- NA works
However, I read somewhere that you should rather use the := notation to do the replacements in place. I am curious for such a solution.

@MichaelChirico
Copy link
Member

MichaelChirico commented Apr 4, 2017

for (jj in seq_len(ncol(maskMatrix))) {
  set(DT, maskMatrix[ , jj], jj, NA)
}

PS you're likely to get a much faster response for this type of question on Stack Overflow

@jangorecki
Copy link
Member

related to #657

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants