[R] A problem with text manipulation

William Dunlap wdunlap at tibco.com
Mon Mar 4 20:31:13 CET 2013


Use ave().  The following tries to save a little time and simplify things
by only processing the duplicated entries.

  > Vec
   [1] "B" "B" "C" "E" "B" "E" "E" "D" "D" "A"
  > f <- function(v) {
  +     d <- duplicated(v)
  +     v[d] <- ave(v[d], v[d], FUN=function(vdi)paste0(vdi, seq_along(vdi)))
  +     v
  + }
  > f(Vec)
   [1] "B"  "B1" "C"  "E"  "B2" "E1" "E2" "D"  "D1" "A"

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of Christofer Bogaso
> Sent: Monday, March 04, 2013 11:14 AM
> To: r-help
> Subject: [R] A problem with text manipulation
> 
> Hello again,
> 
> Let say I have following vector:
> 
> set.seed(1)
> Vec <- sample(LETTERS[1:5], 10, replace = TRUE)
> Vec
> 
> Now with each repeated letter, I like to add suffix programatically.
> Therefore I want to get following vector:
> 
> c("B", "B1", "C", "E", "B2", "E1", "E2", "D", "D1", "A")
> 
> Can somebody tell me how to achieve that?
> 
> Thanks and regards,
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list