getwd()
calcSQRT<- function(arg) {
sqrt <- sqrt(arg)
msg = paste(arg, "の平方根は: ")
paste(msg,sqrt)
}
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
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
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
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
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",]
write.csv(freqMtx,"shiny_freq.csv")
names(freq)
lapply(names(freq), nchar)
sapply(names(freq), nchar)
lapply(names(freq), paste, "@ShintTxt")
sapply(names(freq), paste, "@ShintTxt")
apply(freqMtx, 1, sum)
apply(freqMtx, 2, sum)
apply(freqMtx, c(1,2), sqrt)
apply(freqMtx, c(1,2), function(x) x*10)
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])
###テキストファイル名を引数にして、単語の頻度数と相対頻度をマージした行列データを出力する関数(関数ファイル名: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")