Search code examples
rstringtidyr

How to use tidyr::separate to get only the first element


Let's suppose that I would like to separate a column to three different columns values split by the slash.

df2 <- data.frame(player=c('A', 'A', 'B', 'B', 'C', 'C'),
                     year=c(1, 2, 1, 2, 1, 2),
                     stats=c('22/2/3', '29/3/4', '18/6/7', '11/1/2', '12/1/1', '19/2/4'))

I would use the following code

separate(df, col=stats, into=c('points', 'assists', 'steals'), sep='/')

But if I would like keep just the first element in one single column and the other two separated? I mean something like (example for just the first row):

col1   col2

22     2/3

How should I change the following code?

separate(df2, col=stats, into=c('points', 'assist'), sep='/')

Solution

  • The separate function has an extra= parameter to control what happens when you find more than you are extracting. Use extra="merge" here to collapse the values

    separate(df2, col=stats, into=c('points', 'assist'), sep='/', extra="merge")
    #   player year points assist
    # 1      A    1     22    2/3
    # 2      A    2     29    3/4
    # 3      B    1     18    6/7
    # 4      B    2     11    1/2
    # 5      C    1     12    1/1
    # 6      C    2     19    2/4