2

I have a data set like this:

Quest_main=c("quest2,","quest5,","quest4,","quest12,","quest4,","quest5,quest7")

And I would like to remove the comma from for example "quest2," so that it is "quest2", but not from the "quest5,quest7". I think I have to use substr or ifelse, but I am not sure. The final result is this when I call up Quest_main:

 "quest2"   "quest5"   "quest4"   "quest12"   "quest4"   "quest5,quest7"

Thanks!

3 Answers3

4

All you need is

gsub(",$","",Quest_main)

The $ signifies the end of a string: for full explanation, see the (long and complicated) ?regexp, or a more general introduction to regular expressions, or search for the tags [r] [regex] on Stack Overflow.

If you insist on doing it with substr() and ifelse(), you can:

nc <- nchar(Quest_main)
lastchar <- substr(Quest_main,nc,nc)
ifelse(lastchar==",",substr(Quest_main,1,nc-1),
                     Quest_main)
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • That is a superfast way of doing it. But is it possible with ifelse and substr? –  Oct 18 '14 at 20:44
  • Trik23: See my answer for that. But Ben Bolker's answer is much better :) – Jealie Oct 18 '14 at 20:48
  • 1
    @Trik23 - with `substr` you would first need to find where the `,` occurs, which is much less efficient than `gsub` – Rich Scriven Oct 18 '14 at 20:49
  • 3
    @BenBolker As a beginner in R, I would like to see how different commands function in R. Just personal curiosity and maintaing a learning curve. –  Oct 18 '14 at 20:49
1

Here's an alternative approach (just for general knowledge) using negative lookahead

gsub("(,)(?!\\w)", "", Quest_main, perl = TRUE)
## [1] "quest2"        "quest5"        "quest4"        "quest12"       "quest4"        "quest5,quest7"

This approach is more general in case you want to delete commas not only from end of the word, but specify other conditions too


A more general solution would be using stringis stri_trim_right which will work in cases Bens or Jealie solutions will fail, for example when you have many commas at the end of the sentence which you want to get rid of, for example:

Quest_main <- c("quest2,,,," ,"quest5,quest7,,,,")
Quest_main
#[1] "quest2,,,,"        "quest5,quest7,,,,"
library(stringi)
stri_trim_right(Quest_main, pattern = "[^,]")
#[1] "quest2"        "quest5,quest7"
David Arenburg
  • 91,361
  • 17
  • 137
  • 196
  • but note that `gsub(",+$","",Quest_main)` would work fine for this case. `stringi` is (generally) *faster* but otherwise is just sugar/convenience here. Other `stringi`/`gsub` comparisons: http://stackoverflow.com/questions/26359316/r-removing-numbers-at-begin-and-end-of-a-string/26359372#26359372 , http://stackoverflow.com/questions/26348643/r-regex-with-stringi-icu-why-is-a-considered-a-non-punct-character/26357004#26357004 – Ben Bolker Oct 18 '14 at 22:17
1

With substring and ifelse:

ifelse(substring(Quest_main,nchar(Quest_main))==',',substring(Quest_main,1,nchar(Quest_main)-1),Quest_main)
Jealie
  • 6,157
  • 2
  • 33
  • 36