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

Create third column based on conditions of other column

I am essentially trying to merge information from two columns in the following manner:
if column hf is 0 and hf1 is 1 then create new column combining both information.

df <- structure (list(subject_id = c("191-5467", "191-6784", "191-3457", "191-0987", "191-1245", "191-2365"), hf = c("1","0","0","0","1","0"), hf1 = c("NA","1","1","1","NA","0")), class = "data.frame", row.names = c (NA, -6L))

desired output:

subject_id hf hf1 hf2
191-5467 1 NA 1
191-6784 0 1 1
191-3457 0 1 1
191-0987 0 1 1
191-1245 1 NA 1
191-2365 0 0 0

What I’ve tried:

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

 df$hf2 <- with(df, +(!(df$hf1 == 1 & df$hf1 == 1)))

Output

subject_id hf hf1 hf2
191-5467 1 NA 1
191-6784 0 1 1
191-3457 0 1 1
191-0987 0 1 1
191-1245 1 NA 1
191-2365 0 0 1

Which is close but incorrect because 191-2365 hf2 should have remained 0.

>Solution :

The structure was created with columns as character, It should be numeric. We may use pmax on the numeric columns

df <- type.convert(df, as.is = TRUE)
df$hf2 <- with(df, pmax(hf, hf1, na.rm = TRUE))

-output

> df
  subject_id hf hf1 hf2
1   191-5467  1  NA   1
2   191-6784  0   1   1
3   191-3457  0   1   1
4   191-0987  0   1   1
5   191-1245  1  NA   1
6   191-2365  0   0   0
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