我在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格式:
Vehicle_speed,Time1,Engine_speed,Time2,Engine_torq,Time3,Acc_pedal,Time4,Eng_fuel_rate,Time5
4.98,0,650,0,11,0,0,0,1.15,0
4.98,0,650,0,11,0,0,0,1.2,0.002
4.96,0,650,0.001,11,0.001,0,0.001,1.2,0.003
4.96,0,651,0.001,11,0.001,0,0.001,1.2,0.005
4.94,0.001,651,0.001,11,0.001,0,0.001,1.2,0.007
4.94,0.001,651,0.001,11,0.001,0,0.002,1.2,0.008
4.91,0.001,650.5,0.001,11,0.001,0,0.002,1.2,0.01
4.91,0.001,650.5,0.001,11,0.001,0,0.002,1.2,0.012
4.89,0.001,650.5,0.002,11,0.002,0,0.003,1.15,0.013
4.89,0.001,650.5,0.002,11,0.002,0,0.003,1.15,0.015
4.87,0.002,649.5,0.002,11,0.002,0,0.003,1.15,0.017
4.87,0.002,649.5,0.002,11,0.002,0,0.004,1.15,0.018
4.85,0.002,650,0.002,11,0.002,0,0.004,1.15,0.02
4.85,0.002,650,0.002,11,0.002,0,0.004,1.15,0.022
4.82,0.002,650,0.003,11,0.003,0,0.005,1.2,0.023
从这张表中,我只想找到最发生的发动机转速和车速或大多数发生范围。
答案 0 :(得分:0)
OBD <- read.csv(text = "Vehicle_speed,Time1,Engine_speed,Time2,Engine_torq,Time3,Acc_pedal,Time4,Eng_fuel_rate,Time5
4.98,0,650,0,11,0,0,0,1.15,0
4.98,0,650,0,11,0,0,0,1.2,0.002
4.96,0,650,0.001,11,0.001,0,0.001,1.2,0.003
4.96,0,651,0.001,11,0.001,0,0.001,1.2,0.005
4.94,0.001,651,0.001,11,0.001,0,0.001,1.2,0.007
4.94,0.001,651,0.001,11,0.001,0,0.002,1.2,0.008
4.91,0.001,650.5,0.001,11,0.001,0,0.002,1.2,0.01
4.91,0.001,650.5,0.001,11,0.001,0,0.002,1.2,0.012
4.89,0.001,650.5,0.002,11,0.002,0,0.003,1.15,0.013
4.89,0.001,650.5,0.002,11,0.002,0,0.003,1.15,0.015
4.87,0.002,649.5,0.002,11,0.002,0,0.003,1.15,0.017
4.87,0.002,649.5,0.002,11,0.002,0,0.004,1.15,0.018
4.85,0.002,650,0.002,11,0.002,0,0.004,1.15,0.02
4.85,0.002,650,0.002,11,0.002,0,0.004,1.15,0.022
4.82,0.002,650,0.003,11,0.003,0,0.005,1.2,0.023")
> table(OBD$Engine_speed)
649.5 650 650.5 651
2 6 4 3
或者有几列:
tables <- apply(OBD[ ,c(1,3,5)], 2, table)
> tables
$Vehicle_speed
4.82 4.85 4.87 4.89 4.91 4.94 4.96 4.98
1 2 2 2 2 2 2 2
$Engine_speed
649.5 650 650.5 651
2 6 4 3
$Engine_torq
11
15
只获得最多出现的内容:
> lapply(tables, which.max)
$Vehicle_speed
4.85
2
$Engine_speed
650
2
$Engine_torq
11
1
这是否解决了这个问题?
答案 1 :(得分:0)
要查找最常见的(模式)车速,您可以从table
mySpeeds <- table(df$Vehicle_speed)
modeSpeed <- as.numeric(names(mySpeeds)[which.max(mySpeeds)])
modeSpeed
[1] 4.85
要获得一系列速度的这样的值,您应该使用cut
:
# get range categories
df$speedRange <- cut(df$Vehicle_speed, breaks=c(-Inf, 4.85, 4.90, 4.95, Inf))
mySpeedsRange <- table(df$speedRange)
modeSpeedRange <- names(mySpeedsRange)[which.max(mySpeedsRange)]
modeSpeedRange
[1] "(4.85,4.9]"
cut
接受一个数值变量,并根据第二个(break)参数返回一个因子变量。您可以提供具有单个数字的中断,指示中断的数量或向量,指示唯一的切割点。我添加了-Inf
和Inf
以确保完全覆盖。