[R] String manipulation

jim holtman jholtman at gmail.com
Wed Mar 9 01:48:37 CET 2011


Try this:

> x <- c('ac','ac','c','ac','ac','c')
> rle(x)
Run Length Encoding
  lengths: int [1:4] 2 1 2 1
  values : chr [1:4] "ac" "c" "ac" "c"
> z <- rle(x)
> paste(z$values, ifelse(z$lengths == 1, '', z$lengths), collapse='_', sep = '')
[1] "ac2_c_ac2_c"
>


On Tue, Mar 8, 2011 at 6:33 PM, Denis Kazakiewicz
<d.kazakiewicz at gmail.com> wrote:
> Dear [R] people
> Could you please help with following
>
>
> How to convert a vector
>
> 'ac','ac','c','ac','ac','c'
>
> into a single string
> 'ac2_c_ac2_c'
>
>
> Thank you in advance
>
> ______________________________________________
> 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.
>



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?



More information about the R-help mailing list