Lecture3: 関数の作成

関数ファイルの読み込み

source("calcPower.R")
calcPower(3,3)
## [1] 27

[復習] 単語リストの作成

txt<-readLines("shiny.txt")
wordLst<-strsplit(txt,"[[:space:]]|[[:punct:]]")
wordLst<-unlist(wordLst)
wordLst<-tolower(wordLst)
wordLst<- wordLst[wordLst != ""]
wordLst<- sort(table(wordLst), decreasing=TRUE)
wordLst
## wordLst
##        apps           r       build         can          or       shiny 
##           3           3           2           2           2           2 
##         you           a     actions        also          an         and 
##           2           1           1           1           1           1 
##         css  dashboards   documents        easy       embed      extend 
##           1           1           1           1           1           1 
##        from        host htmlwidgets          in interactive          is 
##           1           1           1           1           1           1 
##          it  javascript       makes    markdown          on     package 
##           1           1           1           1           1           1 
##  standalone    straight        that        them      themes          to 
##           1           1           1           1           1           1 
##         web     webpage        with        your 
##           1           1           1           1

[復習] 単語のToken数

tokens <- length(wordLst)

相対頻度数の計算(小数点2桁)

relative <- wordLst / tokens
(relative <- round(relative,2))
## wordLst
##        apps           r       build         can          or       shiny 
##        0.07        0.07        0.05        0.05        0.05        0.05 
##         you           a     actions        also          an         and 
##        0.05        0.03        0.03        0.03        0.03        0.03 
##         css  dashboards   documents        easy       embed      extend 
##        0.03        0.03        0.03        0.03        0.03        0.03 
##        from        host htmlwidgets          in interactive          is 
##        0.03        0.03        0.03        0.03        0.03        0.03 
##          it  javascript       makes    markdown          on     package 
##        0.03        0.03        0.03        0.03        0.03        0.03 
##  standalone    straight        that        them      themes          to 
##        0.03        0.03        0.03        0.03        0.03        0.03 
##         web     webpage        with        your 
##        0.03        0.03        0.03        0.03

頻度テーブルをデータ型に変換

freqData <- data.frame(wordLst)
#freqData
#relativeData <- data.frame(relative)

相対頻度テーブルをデータ型に変換

(relativeData <- data.frame(relative))
##        wordLst Freq
## 1         apps 0.07
## 2            r 0.07
## 3        build 0.05
## 4          can 0.05
## 5           or 0.05
## 6        shiny 0.05
## 7          you 0.05
## 8            a 0.03
## 9      actions 0.03
## 10        also 0.03
## 11          an 0.03
## 12         and 0.03
## 13         css 0.03
## 14  dashboards 0.03
## 15   documents 0.03
## 16        easy 0.03
## 17       embed 0.03
## 18      extend 0.03
## 19        from 0.03
## 20        host 0.03
## 21 htmlwidgets 0.03
## 22          in 0.03
## 23 interactive 0.03
## 24          is 0.03
## 25          it 0.03
## 26  javascript 0.03
## 27       makes 0.03
## 28    markdown 0.03
## 29          on 0.03
## 30     package 0.03
## 31  standalone 0.03
## 32    straight 0.03
## 33        that 0.03
## 34        them 0.03
## 35      themes 0.03
## 36          to 0.03
## 37         web 0.03
## 38     webpage 0.03
## 39        with 0.03
## 40        your 0.03

2つのデータ型変数を連結(merge)

freqMtx <- merge(freqData, relativeData, all=T, by="wordLst")
head(freqMtx)
##   wordLst Freq.x Freq.y
## 1       a      1   0.03
## 2 actions      1   0.03
## 3    also      1   0.03
## 4      an      1   0.03
## 5     and      1   0.03
## 6    apps      3   0.07

出現単語の情報を行ラベルにコピー

rownames(freqMtx)<-as.character(freqMtx[,1])

出現単語の情報(1列目)を削除

freqMtx<-freqMtx[-1]
colnames(freqMtx) <- c("raw", "relative")
head(freqMtx)
##         raw relative
## a         1     0.03
## actions   1     0.03
## also      1     0.03
## an        1     0.03
## and       1     0.03
## apps      3     0.07

ファイルに出力

write.csv(freqMtx,"freqMtx_shiny.csv")

課題(締め切り:10月28日)

テキストファイル名を引数にして、単語の頻度数と相対頻度をマージした行列データを出力する関数(関数ファイル名:getFreqMtx.R)を作成しなさい。

実行結果

source("getFreqMtx.R")
res<-getFreqMtx("Lec03-text.txt")
head(res)
##        raw   relative
## 2021     1 0.02173913
## a        2 0.04347826
## able     1 0.02173913
## access   1 0.02173913
## also     1 0.02173913
## and      2 0.04347826

manipulate package

インタラクティブなプロット

manipulate packageがインストールされていない場合

install.packages("manipulate")
library(manipulate)

お遊戯

plot(0,0,pch=8)

plot(0,0,pch=8,cex=5)

plot(0,0,pch=8,cex=5,col="red")

色の選択

picker()関数

manipulate(plot(0,0,pch=8,cex=5,col=myColors), myColors=picker("red", "yellow", "green", "violet", "orange", "blue", "pink", "cyan") )

プロットマーカーの選択

picker()関数

manipulate(
  plot(0,0,pch=myMarkers,cex=5,col=myColors), myColors=picker("red", "yellow", "green", "violet", "orange", "blue", "pink", "cyan",initial="violet"),
  myMarkers=picker(1,2,3,4,5,6,7,8,initial="5")
)

プロットサイズの選択

slider()関数

manipulate(
  plot(0,0,pch=8,cex=mySize,col="blue"),
  mySize=slider(1,10,initial=5)
)