【问题标题】:Computing vowel frequency with R用 R 计算元音频率
【发布时间】:2012-06-03 01:17:48
【问题描述】:

您能否告诉我使用 R 计算字符串中元音的绝对频率和相对频率的最佳和最简单的方法是什么?

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

【问题讨论】:

  • 您能否告诉我您的数据是什么样的以及您已经尝试过什么?
  • 你好,Andrie,不是那么多,因为我对 R 很陌生,没有编程经验。我们有一个句子我们首先使用子字符串函数得出字母的数量: letters=substring(s, seq(1,nchar(s),1), seq(1,nchar(s),1)) 现在我们处理 for-each 循环以提取字符串中每个元音的频率,例如: for int i = 0;我
  • { if (x[[i]]=='a'||=='A') a++;..

标签: r string frequency


【解决方案1】:

设置一些数据:

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 

【讨论】:

    猜你喜欢
    • 2015-02-06
    • 1970-01-01
    • 2012-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-18
    • 2017-05-12
    • 2013-04-10
    相关资源
    最近更新 更多