Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Change the values of a column in a specific condition in R

Here, I made 2 columns y and delta

y=matrix(c(1,1,1,0,0,0,1,0),nrow=8)
delta=matrix(c(1,0,0,0,1,0,0,0),nrow=8)

What I want to do now is make new y.
I want to treat delta as an indicator for missing values of y.
If delta is 1, I will treat y as observed.
If delta is 0, I will treat y as missing.
So the expected output for y should be

y=matrix(c(1,NA,NA,NA,0,NA,NA,NA),nrow=8)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

We could use convert the 0 elements in delta to NA and multiply by y

(NA^!delta) * y

-output

     [,1]
[1,]    1
[2,]   NA
[3,]   NA
[4,]   NA
[5,]    0
[6,]   NA
[7,]   NA
[8,]   NA
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading