-2

I'm quite new in R so please forgive me if I don't use the right vocabulary. I am currently dealing with a dataset where I have a few dummy variables. The problem is that for some rows more than 1 dummy variable has a value of 1. If this is the case I would like to set the next dummy variable to NA.

I would like to try something but I don't know how to start. It would be amazing if someone could help me.

Thanks in advance!

Current data

df <- structure(list(Dum_1 = c(1L, NA, NA), Dum_2 = c(NA, 1L, NA), 
    Dum_3 = c(NA, 1L, 1L)), row.names = c(NA, -3L), class = c("tbl_df", 
"tbl", "data.frame"))

# A tibble: 3 x 3
  Dum_1 Dum_2 Dum_3
  <int> <int> <int>
1     1    NA    NA
2    NA     1     1
3    NA    NA     1

Expected output

# A tibble: 3 x 3
  Dum_1 Dum_2 Dum_3
  <int> <int> <int>
1     1    NA    NA
2    NA     1    NA
3    NA    NA     1
Paul
  • 2,877
  • 1
  • 12
  • 28
  • 5
    Welcome to SO. Please share sample data with `dput` and also share the expected output – Sonny Apr 09 '19 at 15:32

1 Answers1

0

assuming your data is stored as a dataframe in an object named df, this should work:

df2 <- apply(df,1,function(x) {
  first_one <- min(which(x == 1))
  x[1:length(x)] <- NA
  x[first_one] <- 1
  print(x)
})

df2

This assumes df looks like this:

df <- tibble::tribble(
  ~`Dum 1`,     ~`Dum 2`,    ~`Dum 3`,
  1,        NA,        NA,
  NA,       1,         1,
  NA,      NA,        1
) %>% as.data.frame()

df

   Dum 1 Dum 2 Dum 3
1     1    NA    NA
2    NA     1     1
3    NA    NA     1
Wil
  • 3,076
  • 2
  • 12
  • 31