用R计算元音频率

van*_*_65 3 string r frequency

你能否告诉我一个使用R来计算字符串中元音的绝对和相对频率的最佳和最简单的方法?

我想源代码与我迄今为止在Java论坛中发现的有点不同.

And*_*rie 13

设置一些数据:

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."
Run Code Online (Sandbox Code Playgroud)

分析一下:

x <- tolower(strsplit(text, "")[[1]])
x <- x[x %in% letters]
Run Code Online (Sandbox Code Playgroud)

绝对频率:

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 
Run Code Online (Sandbox Code Playgroud)

相对频率:

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 
Run Code Online (Sandbox Code Playgroud)