seq(0,10,by=2)
[1] 0 2 4 6 8 10
rep(1:4)
[1] 1 2 3 4
rep(1:4, each=2)
[1] 1 1 2 2 3 3 4 4
rep(1:4, times=2)
[1] 1 2 3 4 1 2 3 4
circle, cardioid, diamond, triangle-forward, triangle, pentagon, star
library(wordcloud2)
wordcloud2(freqData2, size = 0.8, shape = 'star')
numType <- function(value, type=1){
if(type==1){
ans <- value ^ 2
}else if(type==2){
ans <- value ^ 0.5
}
return(ans)
}
numType(100)
[1] 10000
numType(100, type=2)
[1] 10
flagType <- function(value, flag=FALSE){
if(flag==FALSE){
ans <- value ^ 2
}else if(flag==TRUE){
ans <- value ^ 0.5
}
return(ans)
}
flagType(100)
[1] 10000
flagType(100, flag=TRUE)
[1] 10
switchType <- function(value, type="1"){
ans <- switch (type,
"1" = value ^ 2,
"2" = value ^ 0.5
)
return(ans)
}
switchType(100, type="1")
[1] 10000
source("calc_ans.R")
fname <- "sample_texts/sample_en.txt"
words <-tolower(unlist(strsplit(readLines(fname),"[[:space:]]|[[:punct:]]")))
words<- words[words != ""]
typeof(fname)
[1] "character"
typeof(words)
[1] "character"
calcRTTR.new <- function(arg){
print(length(arg))
print(typeof(arg))
if (typeof(arg) != "character") stop("unexpected input")
if(length(arg)==1){
res <- calcRTTR1(arg)
}else if(length(arg)>=2){
res <- calcRTTR2(arg)
}
return(res)
}
calcRTTR.new(fname)
[1] 1
[1] "character"
[1] 6.527299
calcRTTR.new(words)
[1] 71
[1] "character"
[1] 6.527299
calcRTTR.new(1)
[1] 1
[1] "double"
Error in calcRTTR.new(1) : unexpected input
fLst<- list.files("sample_texts")
fLst
[1] "sample_ch.txt" "sample_en.txt" "sample_ja_1.txt" "sample_ja_2.txt"
\[Frequency=\frac{K}{Rank^A} \] K,A: 定数
freqData <- getFreq("GSLC2021.txt")
dim(freqData)
[1] 271 1
head(freqData)
K=freqData[1,1]
A=0.8
rank <- seq(1:dim(freqData)[1])
zipf <- K/rank^A
配置:“bottomright”, “bottom”, “bottomleft”, “left”, “topleft”, “top”, “topright”, “right”, “center”
ラベル
lty: 線の種類
pch: プロットの種類
#理論値
plot(zipf, log="xy", type="l",col="red" ,
xlim=c(1,nrow(freqData)),ylim=c(1,50),main="Zipf's Law", xlab="Rank", ylab="Frequency")
#頻度散布図の重ね書き
par(new=T)
plot(rank,freqData[,1], xlim=c(1,nrow(freqData)), ylim=c(1,50),log="xy",pch=8, col="darkgreen", main="Zipf's Law", xlab="Rank", ylab="Frequency")
#凡例
legend("topright",c("Frequency","Zipf's law"),lty=c(NA,1),pch=c(8,NA),col=c("darkgreen","red"))
plot(zipf, log="xy", type="l",col="red" ,
xlim=c(1,nrow(freqData)),ylim=c(1,50),main="Zipf's Law", xlab="Rank", ylab="Frequency")
par(new=T)
plot(rank,freqData[,1], xlim=c(1,nrow(freqData)), ylim=c(1,50),log="xy",pch=8, col="darkgreen", main="Zipf's Law", xlab="Rank", ylab="Frequency")
legend("topright",c("Frequency","Zipf's law"),lty=c(NA,1),pch=c(8,NA),col=c("darkgreen","red"))