我在3D中有一组点。我想将点占用的整个体积分成128X128X128个多维数据集。我想计算每个立方体中的点数。你能建议如何在R中做到这一点吗?
我已经尝试过rgb和scatterplot3d软件包,但没有在那里找到解决方案。另外,我需要根据它们的数字为这些立方体着色。请建议如何去做。
虽然它不会影响解决方案,但我的数据并非在所有方向上对称分布。因此,将此数量:max(x,y,z)除以128得到每个立方体的边长。
谢谢。
答案 0 :(得分:4)
如果变量x,y和z是坐标向量,那么你可以通过
得到每个坐标的框号xbox <- as.integer(cut(x, breaks=128))
ybox <- as.integer(cut(y, breaks=128))
zbox <- as.integer(cut(z, breaks=128))
希望大多数盒子都是空的,你可以按照
的方式做点什么xyzbox <- paste(xbox, ybox, zbox, sep=":")
table(xyzbox)
为您提供占用方框列表及其点数
答案 1 :(得分:3)
如果你确定要做什么,那么这是一次尝试。
dat <- as.data.frame(replicate(3, runif(10000))) # Toy data
n <- 3 # set to 128, the number of cubes in each direction
cut_dat <- lapply(dat, cut, breaks = seq(0, 1, l = n + 1)) # Break into cubes
do.call(table, cut_dat) # Cross tabulate to count
#, , V3 = (0,0.333]
#
# V2
#V1 (0,0.333] (0.333,0.667] (0.667,1]
# (0,0.333] 383 361 379
# (0.333,0.667] 362 382 371
# (0.667,1] 389 358 370
#
#, , V3 = (0.333,0.667]
#
# V2
#V1 (0,0.333] (0.333,0.667] (0.667,1]
# (0,0.333] 345 383 372
# (0.333,0.667] 374 388 381
# (0.667,1] 383 368 373
#
#, , V3 = (0.667,1]
#
# V2
#V1 (0,0.333] (0.333,0.667] (0.667,1]
# (0,0.333] 342 355 360
# (0.333,0.667] 386 386 374
# (0.667,1] 346 372 357
所以你得到一个带有计数的3d数组。我已经在每个方向上将立方体的数量减少到3来显示概念。
修改:Gavin Kelly的建议也可以在这里完成
table(cut_dat[[1]]:cut_dat[[2]]:cut_dat[[3]])
获取另一个数据表示。
甚至更具人性化:
tab2 <- expand.grid(lapply(cut_dat, levels))
tab2$freq <- table(cut_dat[[1]]:cut_dat[[2]]:cut_dat[[3]])
head(tab, n = 4)
# V1 V2 V3 freq
#1 (0,0.333] (0,0.333] (0,0.333] 374
#2 (0.333,0.667] (0,0.333] (0,0.333] 379
#3 (0.667,1] (0,0.333] (0,0.333] 384
#4 (0,0.333] (0.333,0.667] (0,0.333] 377