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.
Try this: table(strsplit(A, "?")[[1]]) On Tue, Jan 18, 2011 at 10:51 AM, ADias <diasandre@gmail.com> 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. > > ______________________________________________ > R-help@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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Hi r-help-bounces at r-project.org 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 lettersI> 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. Canwe> 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 calculatethe> 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. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
> 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 -- View this message in context: http://r.789695.n4.nabble.com/Relative-frequency-on-a-character-vector-tp3221319p3223904.html Sent from the R help mailing list archive at Nabble.com.
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.DiasAssuming that you want to count only letters (not puctuation) irrespective of capitalization, try this: table(strsplit(tolower(gsub("[^[:alpha:]]", "", A)), "")) Peter Ehlers