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,
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.
Christofer: This reminds me of those IQ puzzles I took so many years ago as a kid: Given the numbers 7, 22, 43, 5, 26,... what are the next 3 numbers in this series? I don't recall having a clue, and when I got older and more mathematical, generally came to the conclusion that it could logically be anything I wanted it to. Anyway... that's my reaction to your post: I haven't a clue what rule you used to construct the desired output from the input. But then others may. So of course I would not be able to tell you **how** to write an R procedure that does it. But then others may. If not, I would suggest that you reveal your secret. Cheers, Bert On Mon, Mar 4, 2013 at 11:13 AM, Christofer Bogaso <bogaso.christofer at gmail.com> wrote:> 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.-- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
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.