Home > Net >  Create third column based on conditions of other column
Create third column based on conditions of other column

Time:12-30

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:

 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.

CodePudding user response:

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
  • Related