R:修订间差异

来自Gea-Suan Lin's Wiki
跳到导航 跳到搜索
此页面具有访问限制。如果您看见此消息,则说明您没有权限访问此页面。
 
(未显示同一用户的3个中间版本)
第1行: 第1行:
'''R'''是一套程式語言,主力在統計運算。
'''R'''是一套程式語言,主力在統計運算。
== 安裝 ==
<syntaxhighlight lang="bash">
sudo apt install -y r-base
</syntaxhighlight>


== 範例 ==
== 範例 ==
先載入ggplot2:
<syntaxhighlight lang="r">
library(ggplot2)
</syntaxhighlight>
接著讀取資料,並且先依照欄位分類:


<syntaxhighlight lang="r">
<syntaxhighlight lang="r">
第7行: 第21行:
colnames(df) <- c('host', 'ident', 'authuser', 'date', 'timezone', 'request', 'status', 'bytes', 'referer', 'useragent', 'cipher')
colnames(df) <- c('host', 'ident', 'authuser', 'date', 'timezone', 'request', 'status', 'bytes', 'referer', 'useragent', 'cipher')
head(df)
head(df)
</syntaxhighlight>
接下來把時間的部份轉成內部用的時間格式:
<syntaxhighlight lang="r">
df$date=as.POSIXct(df$date, tryFormats=("[%d/%b/%Y:%H:%M:%S"))
df$date=as.POSIXct(df$date, tryFormats=("[%d/%b/%Y:%H:%M:%S"))
head(df)
head(df)
</syntaxhighlight>
輸出成PNG圖片:
<syntaxhighlight lang="r">
reqs=as.data.frame(table(df$date))
reqs=as.data.frame(table(df$date))
p <- ggplot(data=reqs, aes(x=as.POSIXct(Var1), y=Freq)) + geom_line() + xlab('Date') + ylab('Requests')
p <- ggplot(data=reqs, aes(x=as.POSIXct(Var1), y=Freq)) + geom_line() + xlab('Date') + ylab('Requests')
第15行: 第39行:
dev.off()
dev.off()
</syntaxhighlight>
</syntaxhighlight>
== 外部連結 ==
* {{Official|https://www.r-project.org/}} {{en}}
* [https://sweetcode.io/r-apache-log-analysis/ R for Apache Log Analysis: Get It Structured] {{en}}
* [https://www.r-bloggers.com/log-file-analysis-with-r/ Log File Analysis with R] {{en}}
[[Category:軟體]]
[[Category:程式語言]]

2020年12月5日 (六) 09:15的最新版本

R是一套程式语言,主力在统计运算。

安装

sudo apt install -y r-base

范例

先载入ggplot2:

library(ggplot2)

接著读取资料,并且先依照栏位分类:

df <- read.table('blog.gslin.org_ssl-access.log.2.gz')
colnames(df) <- c('host', 'ident', 'authuser', 'date', 'timezone', 'request', 'status', 'bytes', 'referer', 'useragent', 'cipher')
head(df)

接下来把时间的部份转成内部用的时间格式:

df$date=as.POSIXct(df$date, tryFormats=("[%d/%b/%Y:%H:%M:%S"))
head(df)

输出成PNG图片:

reqs=as.data.frame(table(df$date))
p <- ggplot(data=reqs, aes(x=as.POSIXct(Var1), y=Freq)) + geom_line() + xlab('Date') + ylab('Requests')
png('a.png', 720, 720)
print(p)
dev.off()

外部连结