[R] A problem with text manipulation
David Winsemius
dwinsemius at comcast.net
Mon Mar 4 21:10:46 CET 2013
On Mar 4, 2013, at 11:31 AM, William Dunlap wrote:
> 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"
And notice that this is the functionality provided by 'make.names' called by the read.* functions:
> make.names(Vec, unique=TRUE)
[1] "B" "B.1" "C" "E" "B.2" "E.1" "E.2" "D" "D.1" "A"
> ?make.names
--
David.
> 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.
>
> ______________________________________________
> 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.
David Winsemius
Alameda, CA, USA
More information about the R-help
mailing list