0

I have a data set as below -

cust  cards
  a  ICICI
  a   HDFC
  a    PNB
  b  ICICI 
  b    PNB
  b   AMEX
  c    RCB
  c   AMEX
  d    PNB

I want one record for each customer so my output should be like

Cust new_cards
a     ICICI,HDFC,PNB
b     ICICI,PNB,AMEX

I am using following command -

x = group_by(data,cust)%>% mutate(new_cards=cat(cards,sep=","))

And getting error. Please let me know how can I get the desired output and if I can do this with function as well?

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Anonamous
  • 253
  • 2
  • 3
  • 14

2 Answers2

1

A slight variation from the previous answer using toString:

library(tidyverse);
df %>%
    group_by(cust) %>%
    summarise(new_cards = toString(cards));
## A tibble: 4 x 2
#  cust  new_cards
#  <fct> <chr>
#1 a     ICICI, HDFC, PNB
#2 b     ICICI, PNB, AMEX
#3 c     RCB, AMEX
#4 d     PNB
Maurits Evers
  • 49,617
  • 4
  • 47
  • 68
0

The correct way of doing this is to use summarize, with paste:

data %>%
    group_by(cust) %>%
    summarize(new_cards = paste(cards, collapse = ', '))

On your example data, this results in

# A tibble: 4 x 2
  cust  new_cards
  <fct> <chr>
1 a     ICICI, HDFC, PNB
2 b     ICICI, PNB, AMEX
3 c     RCB, AMEX
4 d     PNB

The cat function misled you since it’s badly named: it doesn’t concatenate strings, it prints strings (to the terminal or a file).

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214