-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinal.R
More file actions
55 lines (44 loc) · 1.6 KB
/
Copy pathfinal.R
File metadata and controls
55 lines (44 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#第一題
library(tm)
speech<-readLines("Elon_Musk_interview.txt")
corpus<-VCorpus(VectorSource(speech))
corpusSW<-tm_map(corpus, stripWhitespace)
corpusTL<-tm_map(corpusSW, content_transformer(tolower))
corpusRN<-tm_map(corpusTL, removeNumbers)
corpusRP<-tm_map(corpusRN, removePunctuation)
corpusRM<-tm_map(corpusRP, removeWords, stopwords("english"))
tdm<-TermDocumentMatrix(corpusRM)
tdmMatrix<-as.matrix(tdm)
tdmSort<-sort(rowSums(tdmMatrix),T)
tdmdf<-data.frame(freq=tdmSort)
tdmdf_top <- head(tdmdf, 6)
print(tdmdf_top)
barplot(tdmdf_top$freq,
names.arg = rownames(tdmdf_top),
las = 2,
col = "blue",
main = "Top 6 Most Frequent Words",
ylab = "Frequency",
xlab = "Words")
#第二題
library(ggplot2)
MJ<-read.csv("MJ.csv",header=T,sep=",")
KB<-read.csv("KB.csv",header=T,sep=",")
LJ<-read.csv("LJ.csv",header=T,sep=",")
calculate_efficiency <- function(data) {
data$Performance <- data$PTS + data$TRB + data$AST + data$STL + data$BLK - ((data$FGA - data$FG) + (data$FTA - data$FT) + data$TOV)
efficiency <- sum(data$Performance * data$G) / sum(data$G)
return(efficiency)
}
MJ_EFF <- calculate_efficiency(MJ)
KB_EFF <- calculate_efficiency(KB)
LJ_EFF <- calculate_efficiency(LJ)
efficiency_data <- data.frame(
Player = c("MJ", "KB", "LJ"),
Efficiency = c(MJ_EFF, KB_EFF, LJ_EFF)
)
ggplot(efficiency_data, aes(x = Player, y = Efficiency, fill = Player)) +
geom_bar(stat = "identity", position = position_dodge(width = 1)) +
labs(title = "三位球員的生涯績效值", x = "球員", y = "生涯績效值") +
theme_minimal() +
theme(legend.position = "none")