3

I am trying to delete all dashes that appear at the end of a string and my code is causing R to crash. Any tweaks or code fixes that might not cause this would be greatly appreciated!

acct_nm = str_replace_all(acct_nm, "[:punct:]+", "")

Right not I have this wrapped as part of a mutate, unfortunately I cannot show all of my code or include my data. Basically all I want it to replace the acct_nm variable with acct_nm - any trailing punctuation.

As an an example, I want it to work similar to the code below where the variable y gets all trailing punctuation deleted.

library(tidyverse)
test <- data.frame(x = 1:3, y = 'hello-./') %>% 
         mutate(z = str_replace_all(y, "[:punct:]+", ""))

enter image description here

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • 1
    Please make this question *reproducible*. This includes sample *unambiguous* data (e.g., `dput(head(x))` or `data.frame(x=...,y=...)`) and expected output. Refs: https://stackoverflow.com/questions/5963269, https://stackoverflow.com/help/mcve, and https://stackoverflow.com/tags/r/info. – r2evans Nov 08 '19 at 20:14
  • Please show a toy example about your input and expected output – ThomasIsCoding Nov 08 '19 at 20:31
  • (a) Your code replaces all punctuation, not all dashes at the end of a string. Use a pattern like `"-+$"` to match dashes at the end of a string. (b) Since your match is *at the end of a string*, there can only be one match per string. Might as well use `str_replace` rather than `str_replace_all`. (c) Does anything happen before the crash? Are there errors or warnings? Is the crash immediate? How sure are you *this line* causes the crash? – Gregor Thomas Nov 08 '19 at 20:42
  • (d) We don't need *all* of your code or your data, but I don't think we can help much without *a little bit*. Please narrow down the problem and share enough to reproduce. For example, you say this line is wrapped in mutate. Does it still crash if it is not wrapped in mutate? Can you find a subset of your data for which it works? Or, better, a subset of your data for which it doesn't work? Does it crash if you run it on the single string `"alkuo2332----"`? Can you find a small example that reproduces the problem? – Gregor Thomas Nov 08 '19 at 20:42

2 Answers2

7

You can try using sub/gsub and remove punctuations at the end of string

sub('[[:punct:]]+$', '', test$y)
#[1] "-hello" "-hello" "-hello"

data

test <- data.frame(x = 1:3, y = '-hello-./')
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
0

You can try

df$z <- with(df,gsub("(.*?)[[:punct:]]+$","\\1",y))

with the given data frame df <- data.frame(x = 1:3, y = 'hello-./'), then you will get

> df
  x        y     z
1 1 hello-./ hello
2 2 hello-./ hello
3 3 hello-./ hello
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81