4

Similar questions have been asked here, here, and here. However, they don't seem to cover exactly what I need. For example, if I have a dataset like so:

df <- data.frame(
  x = rnorm(10),
  y = rnorm(10),
  a = c(0,0,0,1,1,0,0,0,1,0),
  b = c(1,1,1,1,0,0,1,0,0,0),
  c = c(0,1,0,1,0,0,0,0,0,0),
  z = c(1,1,1,1,1,0,1,0,1,0)
)

What I'm trying to do is convert the variables a, b, and c to a single categorical where the levels are a, b, and c. But as you can see, sometimes 2 variables occur in the same row. So, what I'm trying to achieve is a data frame that would look something like this:

df <- data.frame(
  x = rnorm(10),
  y = rnorm(10),
  a = c(0,0,0,1,1,0,0,0,1,0),
  b = c(1,1,1,1,0,0,1,0,0,0),
  c = c(0,1,0,1,0,0,0,0,0,0),
  z = c(“b”,“b,c”,“b”,“a,b,c”,“a”,0,“b”,0,“a”,0)
)

I tried using :

apply(df[,c("a","b", "c")], 1, sum, na.rm=TRUE)

which sums the amount of each variable... but I'm not sure how to combine 2 (or more) variables into a single factor level!?

Any suggestions as to how I could do this?

1 Answer 1

4

Loop over the selected columns by row (MARGIN = 1), subset the column names where the value is 1 and paste them together

df$z <-  apply(df[c('a', 'b', 'c')], 1, function(x) toString(names(x)[x ==1]))
df$z
#[1] "b"       "b, c"    "b"       "a, b, c" "a"       ""        "b"       ""        "a"       ""       

If we want to change the "" to '0'

df$z[df$z == ''] <- '0'

For a solution with purrr and dplyr:

df %>% mutate(z = pmap_chr(select(., a, b, c), ~  {v1 <- c(...); toString(names(v1)[v1 == 1])}))
5
  • 1
    @GuedesBF Here is a way df %>% mutate(z = pmap_chr(select(., a, b, c), ~ {v1 <- c(...); toString(names(v1)[v1 == 1])}))
    – akrun
    Commented May 4, 2021 at 22:24
  • 1
    Thats it. I was missin the `c(...)' function.
    – GuedesBF
    Commented May 4, 2021 at 22:25
  • @GuedesBF there was a typo. I updatedd it
    – akrun
    Commented May 4, 2021 at 22:25
  • I suggested incorporating that into the answer for future reference
    – GuedesBF
    Commented May 4, 2021 at 22:29
  • 1
    @GuedesBF It's okay. I think I would leave it for others to answer
    – akrun
    Commented May 4, 2021 at 22:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.