Question: I have a data frame which contains a set of keys I want to use in a merging procedure. 
However, the keys are not correctly structured and the data frame looks like that:

```
df <- data.frame("Id" = c("a","b","c","d"), "Keys" = c("001","002/004","003","005/006/007"))

  Id        Keys
1  a         001
2  b     002/004
3  c         003
4  d 005/006/007

```

I want to extract the keys and restructure them in the following way:

```
  Id Keys
1  a  001
2  b  002
3  b  004
4  c  003
5  d  005
6  d  006
7  d  007

```

Has-someone encountered and solved this problem ?

Answer: 
Base R solution, using `strsplit` + `lengths`

```
u <- strsplit(as.character(df$Keys),split = "/")
dfout <- data.frame(ID = rep(df$Id,lengths(u)),
                    Key = unlist(u))

```

such that

```
> dfout
  ID Key
1  a 001
2  b 002
3  b 004
4  c 003
5  d 005
6  d 006
7  d 007

```
In base R:

```r
keys <- strsplit(as.character(df$Keys), "/")
Ids <- rep(df$Id, sapply(keys, length))
data.frame(Id = Ids, Keys = unlist(keys))
#>   Id Keys
#> 1  a  001
#> 2  b  002
#> 3  b  004
#> 4  c  003
#> 5  d  005
#> 6  d  006
#> 7  d  007

```

Created on 2020-02-21 by the [reprex package](https://reprex.tidyverse.org) (v0.3.0)