0

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 ?

Jb_Eyd
  • 635
  • 1
  • 7
  • 20

2 Answers2

1

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
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
0

In base 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 (v0.3.0)

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87