Jump to content

Letter frequency: Difference between revisions

→‎{{header|R}}: Table solution
(Add 8080 assembly version)
(→‎{{header|R}}: Table solution)
Line 4,021:
 
=={{header|R}}==
===Using summary===
<lang R>letter.frequency <- function(filename)
{
Line 4,034 ⟶ 4,035:
- , . ' ( ) [ ] { } < = 1 a c d e f h i l L m n N o p q r s t u U y
22 3 2 1 2 6 6 2 2 1 1 3 1 1 9 6 1 14 7 2 7 8 3 4 6 1 3 3 1 8 8 7 3 1 2 </lang>
===Using table===
R's table function is more idiomatic. For variety, we will use read.delim rather than readLines and show how to only count letters. It is worth noting that readLines is prone to counting empty lines. This may be undesirable.
<lang R>letterFreq<-function(filename,lettersOnly)
{
txt<-read.delim(filename,header = FALSE,stringsAsFactors = FALSE,allowEscapes = FALSE,quote = "")
count<-table(strsplit(paste0(txt[,],collapse = ""),""))
if(lettersOnly){count[names(count)%in%c(LETTERS,letters)]}
else{count}
}</lang>
{{out}}
For fun, we'll use this page for input. However, HTML rarely parses well and the variety of text here is so large that I suspect inaccurate output.
<pre>file<-'https://rosettacode.org/wiki/Letter_frequency'
> letterFreq(file,TRUE)
 
a A b B c C d D e E f F g G h H i I
38186 666 8008 350 16585 1263 4151 277 15020 713 3172 529 3079 149 4549 161 9397 690
j J k K l L m M n N o O p P q Q r R
311 113 3294 76 15906 928 3333 322 26795 355 8926 456 22702 497 1877 39 15055 591
s S t T u U v V w W x X y Y z Z
46527 695 15549 597 5268 269 1003 128 4134 148 1239 144 3037 55 127 77 </pre>
 
=={{header|Racket}}==
331

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.