「R」:修訂間差異
跳至導覽
跳至搜尋
第8行: | 第8行: | ||
== 範例 == | == 範例 == | ||
先載入ggplot2: | |||
<syntaxhighlight lang="r"> | <syntaxhighlight lang="r"> | ||
library(ggplot2) | library(ggplot2) | ||
</syntaxhighlight> | |||
接著讀取資料,並且先依照欄位分類: | |||
<syntaxhighlight lang="r"> | |||
df <- read.table('blog.gslin.org_ssl-access.log.2.gz') | 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') | 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') |
於 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()