0

I have a dataset that goes like this:

    Clubs         Player          Goals
    Barcelona       A                1
    Liverpool       B                2
    M United        C                1
    Arsenal         D | E            1
...

Problem I am facing is in the rows like 4th row. I am thinking, I have to grep for "|". Followed by creating a new row below:

Desired Result for the 4th row is as below:

Arsenal         D             1
Arsenal         E             1

I am unable to do a Grep AND add a new line with the data from above line copied below.

Can someone please share how this can be done? Thank you.

Brijesh
  • 776
  • 5
  • 9
  • [try](http://stackoverflow.com/questions/33571978/split-value-from-a-data-frame-and-create-additional-row-to-store-its-component) [searching](http://stackoverflow.com/questions/30525811/how-to-separate-comma-separated-values-in-r-in-a-new-row) [next](http://stackoverflow.com/questions/33113263/splitting-a-single-column-into-multiple-observation-using-r) [time](http://stackoverflow.com/questions/29758504/split-data-frame-row-into-multiple-rows-based-on-commas) – rawr May 27 '16 at 22:48
  • Thanks for the link Rawr. I searched long and hard but was focused on getting Grep to work on special chars and 'insert' special chars. I had no clue there was a package that could do it. – Brijesh May 27 '16 at 22:58

1 Answers1

0

Seems you want to split |-concatenated data into separate values. And you also want a long data shape (not a wide one). One option:

df <- read.table(header=T,sep=",",text="  Clubs,Player,Goals
Barcelona,A,1
Arsenal,D | E,1")
library(splitstackshape)
cSplit(df, 2, "|", "long")
#        Clubs Player Goals
# 1: Barcelona      A     1
# 2:   Arsenal      D     1
# 3:   Arsenal      E     1

Also see the help file ?cSplit.

lukeA
  • 53,097
  • 5
  • 97
  • 100