getFreqMtx<-function(filename){
txt<-readLines(filename)
wordLst<-strsplit(txt,"[[:space:]]|[[:punct:]]")
....
retrurn(freqMtx)
}
source("getFreqMtx.R")
freqMtx<-getFreqMtx("Lec04-text.txt")
head(freqMtx)
freqMtx<-freqMtx[order(freqMtx$raw, decreasing = TRUE),]
head(freqMtx)
dim(freqMtx)
## [1] 609 2
smallMtx<- freqMtx[freqMtx$raw>=5,]
View(smallMtx)
dim(smallMtx)
## [1] 60 2
smallMtx[1,]
smallMtx[1:2,]
smallMtx[,2]
smallMtx[2,2]
smallMtx[smallMtx$raw >=7 & smallMtx$raw<10,]
smallMtx[rownames(smallMtx)=="covid",]
smallMtx[rownames(smallMtx)==c("osaka","covid"),]
smallMtx[rownames(smallMtx) %in% c("osaka","covid"),]
smallMtx[rownames(smallMtx)=="covid",]
smallMtx[nchar(rownames(smallMtx))==3,]
library(stringr)
smallMtx[str_detect(rownames(smallMtx), "th"),]
smallMtx[str_detect(rownames(smallMtx), "^th"),]
smallMtx[str_detect(rownames(smallMtx), "th$"),]
smallMtx[str_detect(rownames(smallMtx), "^a"),]
smallMtx[str_detect(rownames(smallMtx), "^a|th$"),]
smallMtx[str_detect(rownames(smallMtx), "^t.+th$"),]
#smallMtx[str_detect(rownames(smallMtx), "^t\\w+th$"),]
# 条件文if ## calcSQRT
calcSQRT<- function(value) {
return(sqrt(value))
}
calc2ndPower<- function(value) {
return (value^2)
}
tmp <- 100
calcSQRT(tmp)
## [1] 10
calc2ndPower(tmp)
## [1] 10000
calcTest1 <- function(value, type=1){
if(type==1){
ans = calcSQRT(value)
}else if(type==2){
ans = calc2ndPower(value)
}
return(ans)
}
calcTest1(100)
## [1] 10
calcTest1(100,2)
## [1] 10000
calcTest2 <- function(value, sqrtFlag=FALSE){
if(sqrtFlag==TRUE){
ans = calcSQRT(value)
}else{
ans = calc2ndPower(value)
}
return(ans)
}
calcTest2(100)
## [1] 10000
calcTest2(100, sqrtFlag=TRUE)
## [1] 10
tmp <- smallMtx[1:20,1]
names(tmp) <-rownames(smallMtx)[1:20]
barplot(tmp,las=3)
color8 = c("red", "violet", "pink", "orange", "yellow", "green", "blue", "cyan")
barplot(tmp, las=1,col=color8)
library(manipulate)
picker()関数
manipulate(plot(0,0,pch=8,cex=5,col=myColors), myColors=picker("red", "violet", "pink", "orange", "yellow", "green", "blue", "cyan") )
picker()関数
manipulate(
plot(0,0,pch=myMarkers,cex=5,col=myColors), myColors=picker("red", "violet", "pink", "orange", "yellow", "green", "blue", "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)
)
manipulate(
{
複数行にわたるスクリプト
},
picker, sliderの情報(複数の場合はカンマで結合)
)
tmp <- smallMtx[1:20,1]
names(tmp) <-rownames(smallMtx)[1:20]
color8 = c("red", "violet", "pink", "orange", "yellow", "green", "blue", "cyan")
barplot(tmp, las=1,col=color8)
alt text
\[Frequency=\frac{K}{Rank^A} \] K,A: 定数
K=freqMtx[1,1]
A=0.8
rank <- seq(1:dim(freqMtx)[1])
zipf <- K/rank^A
## [1] 90.00000 51.69143 37.37193 29.68893 24.83513 21.46454
plot(zipf, log="xy", type="l",col="red" ,
xlim=c(1,nrow(freqMtx)),ylim=c(1,50),main="Zipf's Law", xlab="Rank", ylab="Frequency")
par(new=T)
plot(rank,freqMtx[,1], xlim=c(1,nrow(freqMtx)), ylim=c(1,50),log="xy",pch=8, col="darkgreen", main="Zipf's Law", xlab="Rank", ylab="Frequency")
配置:“bottomright”, “bottom”, “bottomleft”, “left”, “topleft”, “top”, “topright”, “right”, “center” ラベル lty: 線の種類 pch: プロットの種類
legend("topright",c("Frequency","Zipf's law"),lty=c(NA,1),pch=c(8,NA),col=c("darkgreen","red"))
K=50
A=0.5
rank <- seq(1:dim(freqMtx)[1])
zipf <- K/rank^A