0

I have a list of words/expressions like this:

df = data.frame(word = c('word','my word', 'another+word-'))

How is it possible to add a specific character after the start and end of word/phrase?

Example of expected results:

df = data.frame(word = c(' word ',' my word ', ' another+word- '))
cottinR
  • 181
  • 1
  • 1
  • 10
  • Take a look at https://stackoverflow.com/questions/28028110/insert-character-at-end-of-string-in-r-except-for-the-last-element for inserting at the end. Currently searching for the begining. – Toodoo Dec 15 '17 at 13:14
  • 5
    Simply `paste0(' ', df$word, ' ')` – Sotos Dec 15 '17 at 13:16
  • regEx style: `sub("(.*)"," \\1 ",df$word)` – Andre Elrico Dec 15 '17 at 13:31
  • 1
    or `paste(NULL, df$word, NULL)` or `sprintf(" %s ", df$word)` – Cath Dec 15 '17 at 13:31
  • With the addition of a couple of reduntant messages :)... Maybe `paste(message(), word, message())`? – Sotos Dec 15 '17 at 13:36
  • 1
    `paste(message("This is a"), df$word, message("very easy question"))` will be better then ;-p – Cath Dec 15 '17 at 13:36
  • @Cath `paste(message("Thank you"), df$word, message("but please don't make fun with someone who tries to learn and what is obvious for you is not for a newbie. A newbie can feel that he is not good for that and as result he will be disappointed and leave his effort to learn and improve himself even through easy questions."))` – cottinR Dec 15 '17 at 23:37
  • No making fun intended, sorry, I was responding to a now deleted comment. Everyone starts by learning. Just a small advice though for next time: SO and google are goldmines to try and find solutions prior to posting a question ;-) – Cath Dec 16 '17 at 14:57

1 Answers1

9

Lots of ways on doing that. Just gathering all the comments:

#Sotos
paste0(' ', df$word, ' ')

#Andre Elrico 1
sub("(.*)"," \\1 ",df$word)
#Andre Elrico 2
suppressMessages(paste(message("very easy question"), df$word, message("very easy question")))

#Cath 1
paste(NULL, df$word, NULL)
#Cath 2
sprintf(" %s ", df$word)

Using stringr:

stringr::str_pad(df$word, width = nchar(df$word) + 2, side = "both")

Benchmarks

library(microbenchmark)
library(stringr)

word <- sapply(sample(3:15, 1e6, replace=TRUE), function(nbchar) paste(sample(letters, nbchar, replace=TRUE), collapse=""))

Sotos <- function(word) paste0(' ', word, ' ')
Andre1 <- function(word) sub("(.*)"," \\1 ", word)
Andre2 <- function(word) suppressMessages(paste(message("very easy question"), word, message("very easy question")))
Cath1 <- function(word) paste(NULL, word, NULL)
Cath2 <- function(word) sprintf(" %s ", word)
strpad <- function(word) str_pad(word, width = nchar(word) + 2, side = "both")

microbenchmark(Sotos(word), Andre1(word), Andre2(word), Cath1(word), Cath2(word), strpad(word), unit="relative")

#         expr       min       lq     mean   median       uq       max neval   cld
#  Sotos(word) 1.2071789 1.190558 1.228668 1.190450 1.245905 1.0740482   100  b   
# Andre1(word) 2.9069459 2.731446 2.690268 2.711295 2.709019 2.1535507   100    d 
# Andre2(word) 0.9999499 1.002723 1.020524 1.000145 1.013569 0.7941186   100 a    
#  Cath1(word) 1.0000000 1.000000 1.000000 1.000000 1.000000 1.0000000   100 a    
#  Cath2(word) 1.4430192 1.403617 1.387463 1.402438 1.400566 1.1396474   100   c  
# strpad(word) 2.7101283 2.599947 2.789208 2.902455 2.942781 2.0281154   100     e
Cath
  • 23,906
  • 5
  • 52
  • 86
Sotos
  • 51,121
  • 6
  • 32
  • 66
  • i have run a speed test: my regex solution using `sub` is the fastest – Andre Elrico Dec 15 '17 at 13:51
  • @AndreElrico what vector did you use ? I used a 5e6 long vector and `sub` sol is one of the slowest. the fastest are solutions with `paste` (so your other and mine) then `paste0` then `sprintf` then `sub` and then `stringr` (but probably it would be faster if the package was loaded). I'll add benchmark – Cath Dec 15 '17 at 15:19