Rinaz Belhaj
Rinaz Belhaj

Reputation: 825

Concatenate column names based on row-wise values

I have an R dataframe with 3 columns containing values 0 or 1. I need to create a column as the concatenation of column names when the value is 1 separated by '&'. The following code works with empty space '' as the separator but fails when I change it to '&'.

Code:

A = c(1,0,1,0,0,1)
B = c(1,1,1,0,1,0)
C = c(0,0,0,1,1,1)
data = data.frame(A, B, C)
data$New = paste(ifelse(data$A == 1, "A", ""),
                 ifelse(data$B == 1, "B", ""),
                 ifelse(data$C == 1, "C", ""), sep = '')
data

Output:

  A B C New
1 1 1 0  AB
2 0 1 0   B
3 1 1 0  AB
4 0 0 1   C
5 0 1 1  BC
6 1 0 1  AC

Code & Output with '&' Separator:

A = c(1,0,1,0,0,1)
B = c(1,1,1,0,1,0)
C = c(0,0,0,1,1,1)
data = data.frame(A, B, C)
data$New = paste(ifelse(data$A == 1, "A", ""), 
                 ifelse(data$B == 1, "B", ""),
                 ifelse(data$C == 1, "C", ""), sep = '&')
data

  A B C  New
1 1 1 0 A&B&
2 0 1 0  &B&
3 1 1 0 A&B&
4 0 0 1  &&C
5 0 1 1 &B&C
6 1 0 1 A&&C

Expected Output:

  A B C New
1 1 1 0 A&B
2 0 1 0   B
3 1 1 0 A&B
4 0 0 1   C
5 0 1 1 B&C
6 1 0 1 A&C
  1. Is there a way to do this in R?
  2. In case of a large number of columns, is there a way to do the same without writing explicit ifelse condition on each column?

Upvotes: 3

Views: 786

Answers (3)

akrun
akrun

Reputation: 887881

We can subset the names by looping through the rows

data$New <- apply(data[1:3], 1, function(x) paste(names(x[x!=0]), collapse="&"))
data$New
#[1] "A&B" "B"   "A&B" "C"   "B&C" "A&C"

it can also be done column wise

library(tidyverse)
data[1:3] %>% 
    na_if(0) %>%
   `*`(col(.)) %>% 
   imap(~ rep(.y, length(.x))[.x]) %>%
   reduce(paste, sep= "&") %>% 
   str_remove("(NA&)+|(&NA)+") %>%
   str_remove("&NA")
#[1] "A&B" "B"   "A&B" "C"   "B&C" "A&C"

Upvotes: 5

Henrik
Henrik

Reputation: 67828

Using which with arr.ind = TRUE, and then aggregate:

cbind(data,
      new = aggregate(col ~ row, data = which(data == 1, arr.ind = TRUE),
                      function(x) paste(names(data)[x], collapse = "&"))[ , "col"])

#   A B C new
# 1 1 1 0 A&B
# 2 0 1 0   B
# 3 1 1 0 A&B
# 4 0 0 1   C
# 5 0 1 1 B&C
# 6 1 0 1 A&C

Similar, using tapply:

ix <- which(data == 1, arr.ind = TRUE)
cbind(data,
      new = tapply(ix[ , "col"], ix[ , "row"],
                   function(x) paste(names(data)[x], collapse = "&")))

Upvotes: 2

Rui Barradas
Rui Barradas

Reputation: 76651

You can use apply with paste to do it.

nms <- names(data)
data$New <- apply(data, 1, function(x){
  paste(nms[as.logical(x)], collapse = "&")
})

data
#  A B C New
#1 1 1 0 A&B
#2 0 1 0   B
#3 1 1 0 A&B
#4 0 0 1   C
#5 0 1 1 B&C
#6 1 0 1 A&C

Upvotes: 4

Related Questions