我有一个速度矢量,另一个有方向。
我想用[0-2,2-4,4-6 ....]的光束对我的速度进行分类,并且每个光束按扇区返回统计汇总。例如:
**梁[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
...
目前,通过使用切割我已经获得:
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)
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)
(45,67.5)(67.5,90)(67.5,90)(45,67.5)
如果我使用table(vel_bins,dir_bins)
,我会得到:
> 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
但我不知道该怎么做。如果有人愿意帮助会非常好! 谢谢!
答案 0 :(得分:0)
我希望你能理解我的代码:
# 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)
希望有所帮助。