Suppose I have the result of a supermarket fruit stock survey stored in a data frame:
stock <- data.frame(
store = c("Asda", "Booths", "Co-op"),
apple = c(1, 0, 0),
banana = c(1, 1, 0),
coconut = c(0, 0, 0)
)
which looks like
store apple banana coconut
1 Asda 1 1 0
2 Booths 0 1 0
3 Co-op 0 0 0
My goal:
I want to convert the above columns of binary survey results into a character vector of stock summary for each supermarket as below:
store fruits
1 Asda apple, banana
2 Booths banana
3 Co-op
My solution:
Step 1: I used a for
loop to replace all 1s in the binary columns with the corresponding column names:
for(i in names(stock)[2:4]) {
stock[which(stock[[i]] == 1), i] <- i
}
and got
store apple banana coconut
1 Asda apple banana 0
2 Booths 0 banana 0
3 Co-op 0 0 0
Step 2: I use tidyr::unite()
to concatenate the individual fruit columns into a character vector column:
library(tidyverse)
stock <- unite(stock, fruits, apple:coconut, sep = ", ")
giving me
store fruits
1 Asda apple, banana, 0
2 Booths 0, banana, 0
3 Co-op 0, 0, 0
Step 3: I had to use stringr::str_replace_all() to remove all unwanted 0s and comma separators:
library(stringr)
stock$fruits <- str_replace_all(stock$fruits, "0, |, 0|0", "")
Although this could get me the result I want, I find my solution rather clumsy, especially the looping part. Could anyone kindly share with me a more efficient and straightforward solution, please? Many thanks in advance!