使用R计算元音频率

4

请问在R中,计算字符串中元音字母的绝对频率和相对频率最好且最简单的方法是什么?

我猜想这个源代码与我在论坛中找到的Java代码有些不同。


请问你能否透露一下你的数据长什么样子以及你已经尝试过哪些方法? - Andrie
你好Andrie,我对R语言非常新手,也没有编程经验。我们有一个句子:我们首先使用substring函数提取了字母的数量:letters=substring(s, seq(1,nchar(s),1), seq(1,nchar(s),1))现在我们正在使用for-each循环来提取字符串中每个元音字母的频率,类似这样:for int i = 0; i < freq; i++ ) { if (x[[1]]=='a'||=='A') a++; 但由于我们都没有编程经验,所以在这里卡住了。 - vanja_65
如果 (x[[i]]=='a'||=='A'),则 a++; - vanja_65
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 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接