Relative frequency on a character vector

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Relative frequency on a character vector

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

Re: Relative frequency on a character vector

Henrique Dallazuanna
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-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Odp: Relative frequency on a character vector

PIKAL Petr
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-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Odp: Relative frequency on a character vector

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

Re: Odp: Relative frequency on a character vector

ADias
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

Reply | Threaded
Open this post in threaded view
|

Re: Relative frequency on a character vector

Peter Ehlers
In reply to this post by ADias
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

______________________________________________
[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.
Reply | Threaded
Open this post in threaded view
|

Re: Odp: Relative frequency on a character vector

Ista Zahn-2
In reply to this post by ADias
Hi,

On Tue, Jan 18, 2011 at 3:48 PM, ADias <[hidden email]> wrote:

>
> 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"?

See ?chartr for character case conversion.

>
> 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?

Seems pretty direct to me... what exactly are you looking for?

>
> Regards,
> A.Dias
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Relative-frequency-on-a-character-vector-tp3221319p3223966.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.
>



--
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org

______________________________________________
[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.