[R] Relative frequency on a character vector
Peter Ehlers
ehlers at ucalgary.ca
Tue Jan 18 22:33:38 CET 2011
On 2011-01-18 04:51, ADias wrote:
>
> Hi,
>
> I have this character vector:
>
> A<-c("Tell me how many different letter this vector has?")
>
> Is there a way with R that it can let me know how many different letters I
> have on this vector?
>
> If I use nchar(A) que gives me the number 50. With this function he is
> counting all the letters present and also spaces between the words. Can we
> also not count the spaces between words?
>
> Then after I knew how many different letters there were on the vector I
> wanted to table the number of times each letter is present to calculate the
> relative frequency of each.
>
> Is this possible with R?
>
> Many thanks
> A.Dias
Assuming that you want to count only letters (not
puctuation) irrespective of capitalization, try this:
table(strsplit(tolower(gsub("[^[:alpha:]]", "", A)), ""))
Peter Ehlers
More information about the R-help
mailing list