Lecture3: 基本統計量, Manipulate関数

作業ディレクトリの確認

getwd()

戻り値なし関数:printSQRT関数をファイルに作成

calcSQRT<- function(arg) {
    sqrt <- sqrt(arg)
    msg = paste(arg, "の平方根は: ")
    paste(msg,sqrt)
}

printSQRT関数ファイルの読み込み

source("calcSQRT.R")
calcSQRT(256)

テキストの頻度表作成(高頻度順)

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

実習1:関数getWordListの作成

テキストファイル名を引数にして、単語の頻度数をテーブルで出力する関数(関数ファイル名:getFreq.R)を作成する。

実行結果

source("getFreq.R")
getFreq("shiny.txt")
## 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

頻度数の集計(頻度順でソート)

freq<-getFreq("shiny.txt")

相対頻度数

全体を1としたときの出現率

relative <- freq / sum(freq)
## wordLst
##        apps           r       build         can          or       shiny 
##  0.06122449  0.06122449  0.04081633  0.04081633  0.04081633  0.04081633 
##         you           a     actions        also          an         and 
##  0.04081633  0.02040816  0.02040816  0.02040816  0.02040816  0.02040816 
##         css  dashboards   documents        easy       embed      extend 
##  0.02040816  0.02040816  0.02040816  0.02040816  0.02040816  0.02040816 
##        from        host htmlwidgets          in interactive          is 
##  0.02040816  0.02040816  0.02040816  0.02040816  0.02040816  0.02040816 
##          it  javascript       makes    markdown          on     package 
##  0.02040816  0.02040816  0.02040816  0.02040816  0.02040816  0.02040816 
##  standalone    straight        that        them      themes          to 
##  0.02040816  0.02040816  0.02040816  0.02040816  0.02040816  0.02040816 
##         web     webpage        with        your 
##  0.02040816  0.02040816  0.02040816  0.02040816
練習:小数点3桁で結果を出力
relative
## wordLst
##        apps           r       build         can          or       shiny 
##       0.061       0.061       0.041       0.041       0.041       0.041 
##         you           a     actions        also          an         and 
##       0.041       0.020       0.020       0.020       0.020       0.020 
##         css  dashboards   documents        easy       embed      extend 
##       0.020       0.020       0.020       0.020       0.020       0.020 
##        from        host htmlwidgets          in interactive          is 
##       0.020       0.020       0.020       0.020       0.020       0.020 
##          it  javascript       makes    markdown          on     package 
##       0.020       0.020       0.020       0.020       0.020       0.020 
##  standalone    straight        that        them      themes          to 
##       0.020       0.020       0.020       0.020       0.020       0.020 
##         web     webpage        with        your 
##       0.020       0.020       0.020       0.020

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

freqData <- data.frame(freq)
relativeData <- data.frame(relative)
##   wordLst Freq
## 1    apps    3
## 2       r    3
## 3   build    2
## 4     can    2
## 5      or    2
## 6   shiny    2
##   wordLst  Freq
## 1    apps 0.061
## 2       r 0.061
## 3   build 0.041
## 4     can 0.041
## 5      or 0.041
## 6   shiny 0.041

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

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

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

rownames(freqMtx)<-as.character(freqMtx[,1])
##         wordLst Freq.x Freq.y
## a             a      1  0.020
## actions actions      1  0.020
## also       also      1  0.020
## an           an      1  0.020
## and         and      1  0.020
## apps       apps      3  0.061

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

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

配列の抽出

freqMtx[1,]
freqMtx[,2]
freqMtx[2,2]
freqMtx$raw
freqMtx[freqMtx$raw==3,]
freqMtx[rownames(freqMtx)=="osaka",]

csvファイルに出力

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

lapply, sapply関数

names(freq)
lapply(names(freq), nchar)
sapply(names(freq), nchar)
lapply(names(freq), paste, "@ShintTxt")
sapply(names(freq), paste, "@ShintTxt")

apply関数

apply(freqMtx, 1, sum)
apply(freqMtx, 2, sum)
apply(freqMtx, c(1,2), sqrt)
apply(freqMtx, c(1,2), function(x) x*10)

mapply関数

mapply(sum, c(1,2,3), c(4,5,6))

同一ディレクトリの複数フォルダから出現単語行列を作成する

ディレクトリ名

dirName <- "testData"

指定ディレクトリのファイル一覧を取得

files <- list.files(dirName)
files
## [1] "test1.txt" "test2.txt" "test3.txt" "test4.txt"

作業ディレクトリからのファイルの相対参照パスを作成

fileDir <- unlist(lapply(dirName, paste, files, sep = "/"))
fileDir
## [1] "testData/test1.txt" "testData/test2.txt" "testData/test3.txt"
## [4] "testData/test4.txt"

ファイルの読み込み

getFreq(filesDir[1])

今日の課題 (締め切り10月29日)

関数getFreqMtxの作成して、動作確認後、関数ファイルをメールで提出すること

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

実行結果出力イメージ

source("getFreqMtx.R")
res<-getFreqMtx("shiny.txt")
head(res)
##       raw   relative
## apps    3 0.06122449
## r       3 0.06122449
## build   2 0.04081633
## can     2 0.04081633
## or      2 0.04081633
## shiny   2 0.04081633

おまけ

txt<-readLines("shiny.txt")
txt[1]
## [1] "Shiny is an R package that makes it easy to build interactive web apps straight from R. "
paste("Line 1: ", txt[1])
## [1] "Line 1:  Shiny is an R package that makes it easy to build interactive web apps straight from R. "
seq(length(txt))
## [1] 1 2 3
tmp<-sapply(seq(length(txt)), function(x) paste(paste("Line", x), ": "))
tmp
## [1] "Line 1 : " "Line 2 : " "Line 3 : "
res<-mapply(paste, tmp, txt)
write(res, "shiny_new.txt")