[R] processing strings in R

Barry Rowlingson B.Rowlingson at lancaster.ac.uk
Fri Oct 6 11:50:57 CEST 2006


Florian Menzel wrote:
> Dear R cracks, 

  We prefer to be called 'R souls'.

>   I need to process data in R which consist of strings like
>    
>   AAABAVVABNN
>   ABVVNNAA
>    
>   What I would like to know is whether there are commands that deliver
>   -the length of a string
>   -one specified character of a string (e.g. the 3rd letter)
>   -that allow to concatenate characters to a string again

  > s1="AAABAVVABNN"
  > s2='ABVVNNAA'

  > nchar(s1)
  [1] 11

  > substr(s1,6,6)
  [1] "V"

  > strsplit(s1,'')
[[1]]
  [1] "A" "A" "A" "B" "A" "V" "V" "A" "B" "N" "N"

  - returns a list.

  > chars = strsplit(s1,'')[[1]]
  > chars
   [1] "A" "A" "A" "B" "A" "V" "V" "A" "B" "N" "N"

  - back together again:

  > paste(chars,collapse='')
  [1] "AAABAVVABNN"



More information about the R-help mailing list