Separate values in a column of a dataframe and melt

Koba

I have a data frame where I want to separate values in the Client.ID column and melt, so each row contains one Client.ID and and the corresponding Account.Name and owner.

> head(df)
  Account.Owner       Account.Name                   Client.ID
1    Deb Berman     Albertsons LLC      3184, 3186, 3185, 2578
2    Deb Berman        All Recipes                   909, 4937
3    Liz Madsen   American Express                   1230,1236
4    Deb Berman  Bed Bath & Beyond                  1180, 1556
5    Deb Berman           Birchbox 101, 1704, 5149, 5150, 5148
6   Jeff Murphy Brown Shoe Company            5402, 6159, 6160

At the end I want it to look like so

Account.Owner       Account.Name                   Client.ID
    1    Deb Berman     Albertsons LLC                  3184  
    2    Deb Berman     Albertsons LLC                  3186
    3    Deb Berman     Albertsons LLC                  3185

Thanks.

A5C1D2H2I1M1N2O1R2T1

I would suggest my cSplit function for a problem like this. The solution becomes:

cSplit(mydf, "Client.ID", ",", "long")
#     Account.Owner       Account.Name Client.ID
#  1:    Deb Berman     Albertsons LLC      3184
#  2:    Deb Berman     Albertsons LLC      3186
#  3:    Deb Berman     Albertsons LLC      3185
#  4:    Deb Berman     Albertsons LLC      2578
#  5:    Deb Berman        All Recipes       909
#  6:    Deb Berman        All Recipes      4937
#  7:    Liz Madsen   American Express      1230
#  8:    Liz Madsen   American Express      1236
#  9:    Deb Berman  Bed Bath & Beyond      1180
# 10:    Deb Berman  Bed Bath & Beyond      1556
# 11:    Deb Berman           Birchbox       101
# 12:    Deb Berman           Birchbox      1704
# 13:    Deb Berman           Birchbox      5149
# 14:    Deb Berman           Birchbox      5150
# 15:    Deb Berman           Birchbox      5148
# 16:   Jeff Murphy Brown Shoe Company      5402
# 17:   Jeff Murphy Brown Shoe Company      6159
# 18:   Jeff Murphy Brown Shoe Company      6160

The arguments used here are: (1) the data.frame or data.table to work with, (2) the column or columns that need to be split up, (3) the separator, and (4) whether the result should be "wide" or "long".

You may want to modify it as follows: cSplit(mydf, "Client.ID", ", ", "long") or cSplit(mydf, "Client.ID", ",|,\\s+", "long", fixed = FALSE) depending on how nice and clean your "Client.ID" column really is.

This assumes we're starting with the following sample dataset:

mydf <- structure(list(Account.Owner = c("Deb Berman", "Deb Berman", 
   "Liz Madsen", "Deb Berman", "Deb Berman", "Jeff Murphy"), 
   Account.Name = c("Albertsons LLC", "All Recipes", "American Express", 
   "Bed Bath & Beyond", "Birchbox", "Brown Shoe Company"), 
   Client.ID = c("3184, 3186, 3185, 2578", "909, 4937", "1230,1236", 
   "1180, 1556", "101, 1704, 5149, 5150, 5148", "5402, 6159, 6160")), 
   .Names = c("Account.Owner", "Account.Name", "Client.ID"), 
   class = c("data.table", "data.frame"), row.names = c(NA, -6L))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Separate values in a column of a dataframe and melt

From Java

Create a new column in a dataframe, based on Groupby and values in a separate column

From Dev

How to melt two dataframe columns into one column

From Dev

Split Pandas Dataframe into separate pieces based on column values

From Dev

Split Pandas Dataframe into separate pieces based on column values

From Dev

pandas - construct column depending on values in 2 separate columns of dataframe

From Dev

How to replace dataframe column with separate dict values - python

From Dev

Looking up for list of variable values in a column in a separate dataframe

From Dev

Pandas - Create Separate Columns in DataFrame Based on a Specific Column's Values

From Dev

pandas DataFrame transpose/melt/pivot for one column only

From Dev

Create a new dataframe by aggregating repeated origin and destination values by a separate count column in a pandas dataframe

From Dev

Displaying Separate Column Values with PHP

From Dev

SUBSTRING And CHARINDEX to separate values in a column

From Dev

Separate a column in dataframe where each observation can have multiple concurrent values

From Dev

R: Extract rows from a dataframe where values from one column occur in a separate vector

From Dev

How to separate values in a column and convert to numeric values?

From Dev

Melt and dcast an awkward dataframe

From Dev

Melt a dataframe by pattern in colnames

From Dev

Melt a dataframe in R

From Dev

melt multiple columns in a dataframe

From Dev

Python: Pandas - Separate a Dataframe based on a column value

From Dev

calculating dataframe row combinations and matches with a separate column

From Dev

reshape dataframe by splitting characters in a column in to separate rows

From Dev

Creating a Separate Column for Prior Week Values (PostgreSQL)

From Dev

Need to count two separate values in one column

From Dev

How to separate boolean column values on mysql

From Dev

How to Melt single column in R

From Java

Replacing column values in a pandas DataFrame

From Dev

Pandas DataFrame column values in to list

Related Related

  1. 1

    Separate values in a column of a dataframe and melt

  2. 2

    Create a new column in a dataframe, based on Groupby and values in a separate column

  3. 3

    How to melt two dataframe columns into one column

  4. 4

    Split Pandas Dataframe into separate pieces based on column values

  5. 5

    Split Pandas Dataframe into separate pieces based on column values

  6. 6

    pandas - construct column depending on values in 2 separate columns of dataframe

  7. 7

    How to replace dataframe column with separate dict values - python

  8. 8

    Looking up for list of variable values in a column in a separate dataframe

  9. 9

    Pandas - Create Separate Columns in DataFrame Based on a Specific Column's Values

  10. 10

    pandas DataFrame transpose/melt/pivot for one column only

  11. 11

    Create a new dataframe by aggregating repeated origin and destination values by a separate count column in a pandas dataframe

  12. 12

    Displaying Separate Column Values with PHP

  13. 13

    SUBSTRING And CHARINDEX to separate values in a column

  14. 14

    Separate a column in dataframe where each observation can have multiple concurrent values

  15. 15

    R: Extract rows from a dataframe where values from one column occur in a separate vector

  16. 16

    How to separate values in a column and convert to numeric values?

  17. 17

    Melt and dcast an awkward dataframe

  18. 18

    Melt a dataframe by pattern in colnames

  19. 19

    Melt a dataframe in R

  20. 20

    melt multiple columns in a dataframe

  21. 21

    Python: Pandas - Separate a Dataframe based on a column value

  22. 22

    calculating dataframe row combinations and matches with a separate column

  23. 23

    reshape dataframe by splitting characters in a column in to separate rows

  24. 24

    Creating a Separate Column for Prior Week Values (PostgreSQL)

  25. 25

    Need to count two separate values in one column

  26. 26

    How to separate boolean column values on mysql

  27. 27

    How to Melt single column in R

  28. 28

    Replacing column values in a pandas DataFrame

  29. 29

    Pandas DataFrame column values in to list

HotTag

Archive