Classify speed beams and sectors
I have a vector of velocities, the other with directions.
I would like to classify my velocities by beams [0-2,2-4,4-6 ....] and for each beam, return a statistical summary by sector. For example:
** beam [2-4] m / s
sector Mean Sigma
345-15: 3.12 0.49
15-45: 3.16 0.52
45-75: 3.10 0.45
...
for now, using the cut I got:
vel_bins: (0.5] (0.5] (0.5) (0.5) (0.5) (0.5)
(5.10) (5.10) (5.10) (5.10) (10.15) (10.15) (10.15)
(10.15) (10.15) (10.15) (10.15)
<p> dir_bins: (67.5, 90) (67.5, 90) (67.5, 90) (67.5, 90) (67.5, 90) (67.5, 90) (67, 5, 90) (67.5, 90) (67.5, 90) (45.67.5) (45.67.5) <p> (45.67.5) (67.5.90) ( 67.5.90] (45.67.5)
And if I use table(vel_bins,dir_bins)
, I get:
> vel_dir_table
vel_bins
dir_bins (0,5] (5,10] (10,15] (15,20] (20,25] (25,30]
(0,22.5] 1956 1122 196 3 0 0
(22.5,45] 2435 2628 618 62 2 0
(45,67.5] 2800 7196 6744 422 5 0
(67.5,90] 2970 5806 3422 16 0 0
(90,112] 2908 2156 381 0 0 0
(112,135] 2031 1221 55 0 0 0
(135,158] 1990 1271 172 8 0 0
(158,180] 1966 1397 274 10 1 0
(180,202] 2272 1509 623 57 13 3
(202,225] 2423 2611 2022 407 26 9
(225,248] 3605 4963 1782 451 64 16
(248,270] 2889 6480 2804 289 21 9
(270,292] 2113 3610 2117 126 11 6
(292,315] 1811 2268 996 102 14 0
(315,338] 2022 1900 1044 69 28 0
(338,360] 1769 1150 258 4 0 0
But I don't know how to follow. If anyone could help, that would be really nice! Thank!
+3
source to share
1 answer
Hope you can understand my code:
# max values, velocity v and position x
max.v <- 12
max.x <- 100
# number of segments
l.v <- 5
l.x <- 4
# random data
data <- data.frame(x=runif(1000, 0, max.x + 3), v=runif(1000, 0, max.v + 3))
dir.v <- seq(0, max.v, length=l.v)
dir.x <- seq(0, max.x, length=l.x)
# create empty matrix of lists
m <- matrix(list(NA), l.x, l.v)
# sort x and v
pos.x <- floor(data$x/max.x * (l.x-1)) + 1
pos.v <- floor(data$v/max.v * (l.v-1)) + 1
# if >l.x then it is put in the last one
pos.x[pos.x>=l.x] <- l.x
pos.v[pos.v>=l.v] <- l.v
# sort
for(i in 1:length(data$v)){
m[[pos.x[i], pos.v[i]]] <- append(m[[pos.x[i], pos.v[i]]], data$v[i])
}
# write to file
fileConn<-file("output.txt", open="w")
for(i in 1:l.v){
write(paste("v", "x", "mean(v)", "\t\tsd(v)", sep="\t"), file=fileConn, append=TRUE)
for(j in 1:l.x){
write(paste(dir.v[i], round(dir.x[j],1), mean(m[[j,i]], na.rm = TRUE), sd(m[[j,i]], na.rm=TRUE), sep="\t"), file=fileConn, append=TRUE)
}
write("\n", file=fileConn, append=TRUE)
}
close(fileConn)
Hope it helps.
0
source to share