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

How to split and paste a string while mutating a dataframe?

$
0
0

I have a dataframe like this one:

x <- data.frame(filename = c("aa-b-c x", "c-dd-e y"), number=c(1,2))
  filename  number
1  aa-b-c x      1
2  c-dd-e y      2

I want to mutate the filename column so it looks like this:

  filename number
1  c/aa/b      1
2  e/dd/c      2

This works on a single row: paste(str_match(x$filename[1], "(\\w+)-(\\w+)-(\\w+)")[c(4,2,3)], collapse = "/") but it fails inside the mutate. I'm sure I'm missing a simple fix.


Viewing all articles
Browse latest Browse all 201839

Trending Articles



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