library(rJava)
library(dplyr)
library(Rwordseg)
library(wordcloud2)
library(stringr)
path
file
#讀取數據
head(QQ)
str(QQ)
#定義:
infor
times
users
# 通過正則表達式提取聊天記錄裡的時間,用戶和聊天消息
for(i in 1:length(QQ)){
reglog
'[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]+:[0-9]+:[0-9]+',QQ[i]) # 給出匹配正則表達式的位置
if(reglog[1]==1){
times[i]
users[i]
}
else {
infor[i]
}
}
#查詢times,users,infor, infor多出一列,刪除第一列
infor
#合併數據:
data
#查看data,有NA數據,進行刪除
head(data)
for(n in 1:dim(data)[1]){
if(is.na(data[n,1])){
if(is.na(data[n,2])){
if(is.na(data)[n,3]){
data
}
}
}
}
#進行分析:
text
text
installDict(dictpath="F:\\\\2-個人資料\\\\R\\\\R-字典\\\\網絡新詞彙.txt",
dictname="網絡新詞彙",dicttype=c("text"))
insterwords
# 插入停用詞
stopwords
wordmessage
wordmessage
wordmessage
# 刪除停用詞
seg_word
# 統計詞頻
p
p=1]
p12)
wordcloud2(p1)
#分析用戶發言時間點
user.time
user.time
user.time.hour
for(i in 1:length(user.time)){
user.time.hour[i]
}
hour
user.time.h
hour
hour
hour$hour
# 發言時間圖:
library(ggplot2)
ggplot(data=hour,aes(x=hour,y=Freq,group=1))+geom_bar(stat='identity',fill="red")+geom_line(color="blue")+labs(main="QQ群發言時間")
# 看了都集中中午發言
# 進行熱力圖生成:
require(plyr)
time
time_table
time_table
day_m
heatmap(day_m,Rowv = NA,Colv = NA,scale="column",col=cm.colors(128),revC=TRUE)
# 1/2/3月份是發言高峰期
# 發言者統計
user.name
as.data.frame(table(user.name))->user.count
user.count.20
user.count.20
p
p+geom_bar(aes(x=reorder(user.name,Freq),y=Freq),stat="identity",fill="red")+coord_flip()
Peter是個積極分子。
本人也榜上有名啊
閱讀更多 阿國 的文章