Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201919

R - split string confidentiality to columns

$
0
0

my dataframe looks like sort of this

df <-
  data.frame(
    id = seq(1, 5, 1),
    acc = c(NA_character_, "115-7981987/7121", "48415875/4874", "8740-454648484/100", "18715/7811")
  )

I'd like to crate 3 new columns from acc column. Im looking for dplyr solution

  1. if there is a - symbol then get value before this symbol split string by / symbol, but do not include part before - symbol (in case that is present)

In other words, result should be exactly:

result <-
  data.frame(
    id = seq(1, 5, 1),
    prefix = c(NA_character_, "115", NA_character_, "8740", NA_character_),
    number = c(NA_character_, "7981987", "48415875", "454648484", "18715"),
    code = c(NA_character_, "7121", "4874", "100", "7811")
  )

Normaly I would use str_split(acc, "/") and then extract parts like this map_chr(uuid, 1) But this not works since some acc fields are empty, and some of them do not include all symbols.


Viewing all articles
Browse latest Browse all 201919

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>