将数字向量分配在其包含的自然序列中的函数
我有一个向量如下:
example <- c(1, 2, 3, 8, 10, 11)
我正在尝试编写一个返回输出的函数,就像您从中获得的函数:
desired_output <- list(first_sequence = c(1, 2, 3),
second_sequence = 8,
third_sequence = c(10, 11)
)
实际上,我想要的是计算我的向量中有多少个序列,以及我的向量中的序列,以及每个长度。碰巧的是,列表作为“ desired_ouput”中的列表就足够了。
最终性是构造另一个向量,我们称其为“ B”,其中包含以下内容:
b <- c(3, 3, 3, 1, 2, 2)
背后的现实世界问题是测量3D PointCloud中包含的3D对象的高度。
我尝试编程一个函数,该函数返回``example_list''中的列表,又是一个直接输出向量“ b”的递归函数,无效。
有人有什么想法吗? 非常感谢。
I have a vector as the following:
example <- c(1, 2, 3, 8, 10, 11)
And I am trying to write a function that returns an output as the one you would get from:
desired_output <- list(first_sequence = c(1, 2, 3),
second_sequence = 8,
third_sequence = c(10, 11)
)
Actually, what I want is to count how many sequences as of those there are in my vector, and the length of each one. It just happens that a list as the one in "desired_ouput" would be sufficient.
The finality is to construct another vector, let's call it "b", that contains the following:
b <- c(3, 3, 3, 1, 2, 2)
The real world problem behind this is to measure the height of 3d objects contained in a 3D pointcloud.
I've tried to program both a function that returns the list in "example_list" and a recursive function that directly outputs vector "b", succeeded at none.
Someone has any idea?
Thank you very much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我们可以通过通过
diff
相邻元素创建分组来拆分为list
,然后我们获得
lengths
andrep 代码>丽丝
We can split to a
list
by creating a grouping bydiff
erence of adjacent elementsThen, we get the
lengths
andrep
licate您可以做:
要获得长度:
You could do:
To get the lengths:
这是另一个。 而是一种不同的方法:
Here is one more. Not elegant but a different approach:
tapply
另一个选项是使用
ave
:One other option is to use
ave
: