[R] A problem with text manipulation

arun smartpink111 at yahoo.com
Mon Mar 4 21:05:19 CET 2013


Hi,
You could use:


res<-unsplit(lapply(split(Vec,Vec),function(x) if(length(x)>1) c(head(x,1),paste0(head(x,-1),seq_along(head(x,-1)))) else x),Vec)
 res
# [1] "B"  "B1" "C"  "E"  "B2" "E1" "E2" "D"  "D1" "A" 
A.K.



----- Original Message -----
From: Christofer Bogaso <bogaso.christofer at gmail.com>
To: r-help <r-help at r-project.org>
Cc: 
Sent: Monday, March 4, 2013 2:13 PM
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