为什么rep()与这个简单的R例子行为不一致?(why does rep() behave inconsistently with this simple R example?)
我正在构建代码来运行和管理可以在三个站点队列之一的站点上的抽样事件的模拟。 我使用
rep()
)使用以下代码分配同类群组标识符(1,2或3):cohort <- rep(1:n.cohorts, n.sites)
我已经把关键行放在首位,虽然为了重现我的问题,你需要运行以下行,这些行分配队列之间的站点总数以便呈现给
rep()
调用。n.cohorts <- 3 s <- 10 # total available sites in this example # different proportions of the total can be allocated to each cohort, for example prop.control <- 0.4 ; prop.int <- 0.4 ; prop.ref <- 1-(prop.int+prop.control) n.control <- prop.control * s; n.int <- prop.int * s; n.ref <- prop.ref * s n.sites <- c(n.control, n.int, n.ref)
现在,
n.sites
本身就会回归[1] 4 4 2
因此当我运行我的
cohort <- rep(1:n.cohorts, n.sites)
再次呼叫时,我希望cohort <- rep(1:n.cohorts, n.sites)
cohort
成为10个项目的列表,如下所示:[1] 1 1 1 1 2 2 2 2 3 3
。 然而,我得到的只有9:> cohort [1] 1 1 1 1 2 2 2 2 3
如果我运行相同的代码,其中
n.sites
直接定义如下:n.sites <- c(4, 4, 2)
,我得到了我期望的10个项目。 我已经多次重做这件事来说服自己,在两种情况下,n.sites
本身都会产生相同的结果。谁能解释为什么会这样? 提前谢谢了。
大卫
I am building code to run and manage simulations of sampling events at sites that can be in one of three site cohorts. I use
rep()
to assign the cohort identifier (1,2, or 3) using the code below:cohort <- rep(1:n.cohorts, n.sites)
I have put the key line first, although to reproduce my problem you need to run the following lines, which allocate a total number of sites between cohorts for presentation to the
rep()
call.n.cohorts <- 3 s <- 10 # total available sites in this example # different proportions of the total can be allocated to each cohort, for example prop.control <- 0.4 ; prop.int <- 0.4 ; prop.ref <- 1-(prop.int+prop.control) n.control <- prop.control * s; n.int <- prop.int * s; n.ref <- prop.ref * s n.sites <- c(n.control, n.int, n.ref)
now,
n.sites
by itself returns[1] 4 4 2
so when I run my
cohort <- rep(1:n.cohorts, n.sites)
call again I expectcohort
to be a list of 10 items, like this:[1] 1 1 1 1 2 2 2 2 3 3
. What i get, however, is only 9:> cohort [1] 1 1 1 1 2 2 2 2 3
If i run the same code where
n.sites
is defined directly like so:n.sites <- c(4, 4, 2)
, I get the 10 items i expect. I have redone this several times to convince myself that under both scenariosn.sites
by itself produces identical results.Can anyone explain why this happens? Many thanks in advance.
David
原文:https://stackoverflow.com/questions/21398586
最满意答案
也许这更像你的意图:
Robot.prototype.draw = function() { for (x in this.parts){ this.sc.add(this.parts[x]); // < - spot the difference :) alert("hit"); } }
Maybe this works more like you intended:
Robot.prototype.draw = function() { for (x in this.parts){ this.sc.add(this.parts[x]); // < - spot the difference :) alert("hit"); } }
相关问答
更多-
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
在three.js中,对象的“中心”是对象的局部坐标系中的原点。 如果对象没有围绕所需的点旋转,则需要平移几何体的顶点,以使所需的中心位于原点。 geometry.translate( distX, distY, distZ ); 有关更多信息和备用解决方案,请参阅此答案 。 three.js r.78 In three.js, an object's "center" is the origin in the object's local coordinate system. If your objec ...
-
您已经下载了最新版本的three.js库(可能是r68),但是这些示例是针对r63版本编写的,所以有些东西已经改变了。 正如您在r67中的https://github.com/mrdoob/three.js/releases所见,方法computeCentroids已从Geometry中删除。 You have downloaded the latest version of the three.js library (probably r68) but the examples are written a ...
-
我挖进动画并注意到它使用了morphTargets 。 然后我想起了这个例子 。 因此,关键时刻是将.morphTarget参数设置为true ,因此,我已将其应用于代码段中的材质并开始工作: mesh.material = new THREE.MeshPhongMaterial({ map: tgaLoader.load('//cdn.rawgit.com/PiranhaGreg/files/master/SCA_BODY_V0.TGA'), morphTargets: true }); ...
-
也许这更像你的意图: Robot.prototype.draw = function() { for (x in this.parts){ this.sc.add(this.parts[x]); // < - spot the difference :) alert("hit"); } } Maybe this works more like you intended: Robot.prototype.draw = function() { for (x in this.par ...
-
如果你想要相同网格的N个实例,你不需要加载它们N次; 只需加载一次,然后创建具有相同几何体的N个新网格。 我对类似问题的回答是: 如何在threejs中克隆collada模型? 希望这可以帮助 If you want N instances of the same mesh, you don't need to load them N times; just load it once, and create N new meshes with the same geometry. My answer on ...
-
OOP JavaScript + Three.js:OrbitControls无效(OOP JavaScript + Three.js: OrbitControls not working)[2023-05-13]
您遇到的错误仅来自显示的1帧。 您需要使渲染循环递归。 你有 requestAnimationFrame(ixmodel.render); 评论说。 这将允许您显示更新。 取消注释,它应该工作。 The error you're having comes from only 1 frame being displayed. You need to make your render loop recursive. You have requestAnimationFrame(ixmodel.render); ... -
这是当前three.js和那个threex使用的代码之间的版本不兼容。 Threex与一年前的three.js版本兼容。 学习three.js时,这有很好的建议: 学习WebGL和three.js https://github.com/jeromeetienne/threex/tree/master/vendor中可以看到threex使用的three.js版本 This is a version incompatibility between current three.js and the code us ...
-
调试Three.js的技巧(Technique for debugging Three.js)[2023-10-29]
实际上这就是我想调试代码时的工作。 交换三个.min.js并输入three.js。 缩小版本包含相同的代码。 该文件的非缩小版本受版本控制: https://github.com/mrdoob/three.js/tree/master/build Actually that is what I do when I want to debug my code. Swap out three.min.js and put in three.js. The minified version contains th ... -
你试图传递材料清单而不告诉three.js这是一个多材料。 改变这一行: self._mesh = new THREE.Mesh( geometry , materials ); 至: var materialSet = new THREE.MultiMaterial( materials ); self._mesh = new THREE.Mesh( geometry , materialSet ); 现在您正在使用正确的json提供的材质,您需要为场景添加灯光,否则模型中的lambert材质将不会显 ...