separate_rows

0th

Percentile

Separate a collapsed column into multiple rows.

If a variable contains observations with multiple delimited values, this separates the values and places each one in its own row.

Usage
separate_rows(data, ..., sep = "[^[:alnum:].]+", convert = FALSE)
Arguments
data
A data frame.
...
Specification of columns to separate. Use bare variable names. Select all variables between x and z with x:z, exclude y with -y. For more options, see the select documentation.
sep
Separator delimiting collapsed values.
convert
If TRUE, will run type.convert with as.is = TRUE on new columns. This is useful if the component columns are integer, numeric or logical.
Aliases
  • separate_rows
Examples

df <- data.frame(
  x = 1:3,
  y = c("a", "d,e,f", "g,h"),
  z = c("1", "2,3,4", "5,6"),
  stringsAsFactors = FALSE
)
separate_rows(df, y, z, convert = TRUE)
Documentation reproduced from package tidyr, version 0.6.1, License: MIT + file LICENSE

Community examples

rachhpal1176 at Feb 4, 2019 tidyr v0.8.2

## Separate column into rows splitting the column values with delimiter game <- c('Cricket', 'Hockey') player <- c('Dhruv, Gurnoor and Rachpal', 'Pargat, Sardar and Dhyan.Chand') roster_df <- data.frame(Game=game, Player=player) # Option 1 roster_df %>% separate_rows(Player, sep = ", | and " , convert = FALSE) # Option 2 roster_df %>% mutate(Player = str_split(Player, ", | and ")) %>% unnest() # Option 3 roster_df %>% unnest(Player = str_split(Player, ", | and "))