4

您能否给我一个提示,说明使用 R 计算字符串中元音的绝对频率和相对频率的最佳和最简单的方法是什么?

我猜源代码与我目前在论坛上找到的有关 Java 的内容有点不同。

4

1 回答 1

14

设置一些数据:

text <- "Can you please give me a hint on what's the best and easiest way to compute the absolute and relative frequencies of vowels in a string using R?

I guess the source code is a bit different from what I've found so far in the forum concerning Java.

Any help is appreciated."

分析一下:

x <- tolower(strsplit(text, "")[[1]])
x <- x[x %in% letters]

绝对频率:

table(x)
x
 a  b  c  d  e  f  g  h  i  j  l  m  n  o  p  q  r  s  t  u  v  w  y 
19  3  8  6 29  8  5  8 17  1  5  4 16 14  5  1 11 16 17  9  5  4  3 

相对频率:

table(x)/length(x)
x
          a           b           c           d           e           f           g           h           i 
0.088785047 0.014018692 0.037383178 0.028037383 0.135514019 0.037383178 0.023364486 0.037383178 0.079439252 
          j           l           m           n           o           p           q           r           s 
0.004672897 0.023364486 0.018691589 0.074766355 0.065420561 0.023364486 0.004672897 0.051401869 0.074766355 
          t           u           v           w           y 
0.079439252 0.042056075 0.023364486 0.018691589 0.014018692 
于 2012-05-28T17:59:19.800 回答