Home > database >  Iterating over columns to create flagging variables
Iterating over columns to create flagging variables

Time:10-22

I've got a dataset that has a lot of numerical columns (in the example below these columns are x, y, z). I want to create individual flagging variables for each of those columns (x_YN, y_YN, z_YN) such that, if the numerical column is > 0, the flagging variable is = 1 and otherwise it's = 0. What might be the most efficient way to tackle this?

Thanks for the help!

x <- c(3, 7, 0, 10)
y <- c(5, 2, 20, 0)
z <- c(0, 0, 4, 12)

df <- data.frame(x,y,z)

CodePudding user response:

We may use a logical matrix and coerce

df[paste0(names(df), "_YN")] <-  (df > 0)

-output

> df
   x  y  z x_YN y_YN z_YN
1  3  5  0    1    1    0
2  7  2  0    1    1    0
3  0 20  4    0    1    1
4 10  0 12    1    0    1

CodePudding user response:

The dplyr alternative:

library(dplyr)
df %>% 
  mutate(across(everything(), ~  (.x > 0), .names = "{col}_YN"))

output

   x  y  z x_YN y_YN z_YN
1  3  5  0    1    1    0
2  7  2  0    1    1    0
3  0 20  4    0    1    1
4 10  0 12    1    0    1
  • Related