# Relative frequency on a character vector

7 messages
Open this post in threaded view
|

## Relative frequency on a character vector

 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
Open this post in threaded view
|

## Re: Relative frequency on a character vector

 Try this: table(strsplit(A, "?")[[1]]) On Tue, Jan 18, 2011 at 10:51 AM, ADias <[hidden email]> 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 > -- > View this message in context: > http://r.789695.n4.nabble.com/Relative-frequency-on-a-character-vector-tp3221319p3221319.html> Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > [hidden email] 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. > -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Odp: Relative frequency on a character vector

 In reply to this post by ADias Hi [hidden email] napsal dne 18.01.2011 13:51:05: > > 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? A<-c("Tell me how many different letter this vector has?") x<-strsplit(A, "") > x [[1]]  [1] "T" "e" "l" "l" " " "m" "e" " " "h" "o" "w" " " "m" "a" "n" "y" " " "d" "i" [20] "f" "f" "e" "r" "e" "n" "t" " " "l" "e" "t" "t" "e" "r" " " "t" "h" "i" "s" [39] " " "v" "e" "c" "t" "o" "r" " " "h" "a" "s" "?" > table(x) x   ? a c d e f h i l m n o r s t T v w y 8 1 2 1 1 7 2 3 2 3 2 2 2 3 2 5 1 1 1 1 > length(table(x)) [1] 20 > Regards Petr BTW. There are help pages coming with R installation. Do you have them corrupted? There is strsplit in see also section of nchar help page. > > 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 > -- > View this message in context: http://r.789695.n4.nabble.com/Relative-> frequency-on-a-character-vector-tp3221319p3221319.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > [hidden email] 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. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Odp: Relative frequency on a character vector

 BTW. There are help pages coming with R installation. Do you have them corrupted? There is strsplit in see also section of nchar help page. yes I do. But I have many dificulties in finding what I need. And on top of that R has a very specific way of working that is quite diferent from what I am used to with other softwares. So I get really lost sometimes. But I am getting better and I more and more find what I need just researching the help pages. many thanks Regards, ADias
Open this post in threaded view
|

## Re: Odp: Relative frequency on a character vector

 I have done like this to get the result I need more directly A<-c("Tell me how many different letter this vector has?") prop.table(table(strsplit(A,"")))         ?    a    c    d    e    f    h    i    l    m    n    o    r    s    t 0.16 0.02 0.04 0.02 0.02 0.14 0.04 0.06 0.04 0.06 0.04 0.04 0.04 0.06 0.04 0.10    T    v    w    y 0.02 0.02 0.02 0.02 two questios: 1) is there a direct way of making capital "T" the same as lower case "t"? 2) And can I avoid on the strslipt function to exclude the "?" and the spaces between words? I know I can do: x<-table(strsplit(A, "")) ] > x a c d e f h i l m n o r s t T v w y 2 1 1 7 2 3 2 3 2 2 2 3 2 5 1 1 1 1 My question is always to find out if there's a more direct way of doing this? Regards, A.Dias