トップ 差分 一覧 Farm ソース 検索 ヘルプ PDF RSS ログイン

sapply

*disclaimer
120103

[R]
R

sapply


sapply(データ, 命令)
  • データ内の各要素に対して命令を実行する

  • 一文一行のデータ(ベクトルデータとして一文が一要素として入っている)
  • 文字数を数える命令 nchar()

  例:ns502

> head(ns502)
[1] "An Assumed Role"                                                                                                                                                                                                                    
[2] "Considering the heightened role maintained by many in education, it's strangely rare to fin many willing to question this status quo."                                                                                              
[3] "Of course, many aware of the dynamic contrast between student and teacher are perfectly willing to perpetuate and even strengthen this relationship."                                                                               
[4] "It may seem entirely silly, even, to consider that anything needs to be changed."                                                                                                                                                   
[5] "However, with growing competition in workplaces and with newer jobs being developed on a regular basis, it may be necessary to reexamine this two-dimensional hierarchy in order to better prepare students for the changing world."
[6] "For years, American schools have maintained a strict adherence to an invisible, ghostly code, one that encourages respect and honor in a manner reminiscent of both the power structure found in factories and the military." 

各文の文字数を数える

> sapply(ns502, nchar)
An Assumed Role 
15 
Considering the heightened role maintained by many in education, it's strangely rare to fin many willing to question this status quo. 
133 
Of course, many aware of the dynamic contrast between student and teacher are perfectly willing to perpetuate and even strengthen this relationship. 
148 
It may seem entirely silly, even, to consider that anything needs to be changed. 
80 
However, with growing competition in workplaces and with newer jobs being developed on a regular basis, it may be necessary to reexamine this two-dimensional hierarchy in order to better prepare students for the changing world. 
227 
For years, American schools have maintained a strict adherence to an invisible, ghostly code, one that encourages respect and honor in a manner reminiscent of both the power structure found in factories and the military. 
220

出てくる文字数を合計して、文の数で割ったら、一文当たりの平均文字数

> sum(sapply(ns502, nchar))/length(ns502)
[1] 153.9231
  • ただし、この状態では、半角スペースも一文字