R拆分访问列

时间:2015-02-15 22:35:02

标签: r

我在stats_sample.csv中有这个示例数据:

IP,Timestamp,statistic
IP1,20150215204928,1.100000
IP2,20150215204935,1.300000
IP1,20150215204936,0.200000
IP3,20150215204938,2.900000
IP1,20150215204942,1.800000
IP1,20150215204949,1.600000
IP4,20150215204949,2.700000
IP2,20150215204949,1.100000
IP2,20150215204951,3.000000
IP1,20150215204952,1.600000
IP3,20150215204958,0.600000
IP4,20150215204959,0.000000

我想创建一个情节,我将为每个IP一行 - x将是时间戳,y是统计数据。这是我的R脚本:

data <- read.csv("stats_sample.csv", sep=",", head=TRUE)
data <- split(data, data$IP)

png("plot.png")
plot.new()
for (i in 1:length(data)) {
   lines(data[i]$Timestamp, data[i]$statistic)
}
dev.off()

结果我得到一个空的情节。

当我打印数据[1]时,我得到了这个:

$IP1
    IP    Timestamp statistic
1  IP1 2.015022e+13       1.1
3  IP1 2.015022e+13       0.2
5  IP1 2.015022e+13       1.8
6  IP1 2.015022e+13       1.6
10 IP1 2.015022e+13       1.6

我怎么能让这项工作好吗?

感谢您的任何努力。

P.S:我的R技能相当糟糕。

1 个答案:

答案 0 :(得分:3)

您应首先将数字Timestamp转换为日期时间格式,然后在使用多个plot之前使用lines启动绘图:

data$Timestamp <-   as.POSIXct(as.character(data$Timestamp), format = "%Y%m%d%H%M%S")
png("plot.png")
with(data, { 
  plot(0, xlim = range(Timestamp), ylim = range(statistic), xaxt = "n")
  axis(1, at = pretty(Timestamp), labels = pretty(Timestamp))
})
invisible(lapply(split(data, data$IP), function(dat) {
  with(data, lines(Timestamp, statistic, col = IP))
}))
dev.off()

或者,您可以使用ggplot

更轻松
library(ggplot2)
ggsave(filename = "plot.png", 
  ggplot(data, aes(x = Timestamp, y = statistic, colour = IP)) + 
    geom_line()
)