如何在R中创建类和频率表

时间:2016-06-28 05:14:38

标签: r csv frequency

我在R中绘制了一个图表:

OBD=read.csv("OBD.CSV",header = TRUE,stringsAsFactors=FALSE)
x1 <- OBD$Time1
x2 <- OBD$Time2
y1<-OBD$Vehicle_speed
y2 <-OBD$Engine_speed
par(mar=c(5,4,4,5)+.1)
plot(x1,y1,type="l",col="yellow",ylab = "Vehicle speed")
par(new=TRUE)
plot(x2,y2,type="l",col="blue4",xaxt="n",yaxt="n",xlab="Time",ylab="")
axis(4)
mtext("Engine speed",side=4,line=3)
legend("topleft",col=c("blue4","yellow"),lty=1,legend=c("y1","y2"))

我的CSV文件包含82698行和10列:

Engine_speed    Time2
650        0
650        0
650     0.001
651     0.001
600.5   0.202
600.5   0.202
600.5   0.202
600     0.203
600     0.203
600.5   0.203
1795    0.888
1800    0.888
1796    0.888
1800    0.888
1808.5  0.889
2038.5  1.217
2032    1.218
2036.5  1.218
2037.5  1.218
2028    1.218
2032.5  1.218
599.5   0.728
599.5   0.728
600     0.728
600     0.728
600     0.728
599.5   0.728
599.5   0.729
599.5   0.729
599.5   0.729
599.5   0.729

我想获得min_engine速度,最大发动机转速,标准偏差和频率。

Engine Speed    Frequency
Below 600   6818
600-800    12014
800-1000    2952
1000-1200   4443
1200-1400   7824
1400-1600   9969
1600-1800   12682
1800-2000   6794
2000-2200   9922
2200-2400   3790
2400-2600   5197
2600-2800   293

我在excel中获得了类和频率(我想获得模式,我认为R没有内置模式)。我也希望在R中制作类似的表格。我尝试使用数据帧。

Mode <- function(MD) 
  {
MDX <- unique(MD)
MDX[which.max(tabulate(match(MD, MDX)))]
}

table(OBD$Engine_speed)

我是R.的新手。请帮助我

1 个答案:

答案 0 :(得分:1)

我们可以使用data.table

library(data.table)
setDT(df1)[, .N,
     .(Engine_speed = cut(Engine_speed, breaks = seq(400, 2800, by = 200)))]

或使用base R

aggregate(Time2~Engine_Speed, transform(df1, Engine_Speed = cut(Engine_speed,
           breaks = seq(400, 2800, by = 200))), FUN = length)