从追加回路RLE结果 [英] Append rle result from loop
问题描述
我运行一个硬币抛仿真它运行约100万次的循环。
I am running a coin-toss simulation with a loop which runs about 1 million times.
我每次运行循环我希望保留从RLE命令的输出表。不幸的是,简单的追加似乎并不合适。每次我跑环,我得到一个稍微不同的数据量,这似乎是症结之一。
Each time I run the loop I wish to retain the table output from the RLE command. Unfortunately a simple append does not seem to be appropriate. Each time I run the loop I get a slightly different amount of data which seems to be one of the sticking points.
这code给我在做什么的想法:
This code gives an idea of what I am doing:
N <- 5 #Number of times to run
rlex <-NULL
#begin loop#############################
for (i in 1:N) { #tells R to repeat N number
x <-sample(0:1, 100000, 1/2)
rlex <-append(rlex, rle(x))
}
table(rlex) #doesn't work
table(rle(x)) #only 1
因此,不是有五个独立的RLE结果(此模拟,在完整版1元),我想要一个合并RLE表。希望这是显而易见的。显然,我的实际code是更复杂一点,因此任何解决方案应尽可能接近我已指定越好。
So instead of having five separate rle results (in this simulation, 1 million in the full version), I want one merged rle table. Hope this is clear. Obviously my actual code is a bit more complex, hence any solution should be as close to what I have specified as possible.
更新:该环是一个绝对的要求。没有如果或但是。也许我可以拉出表(RLE(X))的数据,并把它变成一个矩阵。但是再绊脚石的事实是,一些不太频繁的运行长度并不总是在每个循环转动起来。因此,我想我希望有条件填补基础上,运行长度数矩阵?
UPDATE: The loop is an absolute requirement. No ifs or buts. Perhaps I can pull out the table(rle(x)) data and put it into a matrix. However again the stumbling block is the fact that some of the less frequent run lengths do not always turn up in each loop. Thus I guess I am looking to conditionally fill a matrix based on the run length number?
最后更新之前我放弃了:保留了RLE $值将意味着太多的数据被保留。我的模拟是大规模,我真的只希望保留RLE的表输出。要么我保留每个表(RLE(X))为每个循环和手工相结合(会有成千上万的),或者我找一个纲领性的方式来保存数据(是的零和的),并具有从形成一个表合并每个单独的回路,因为我一起去。
Last update before I give up: Retaining the rle$values will mean that too much data is being retained. My simulation is large-scale and I really only wish to retain the table output of the rle. Either I retain each table(rle(x)) for each loop and combine by hand (there will be thousands), or I find a programmatic way to keep the data (yes for zeroes and ones) and have one table that is formed from merging each of the individual loops as I go along.
无论这是easyish做的,符合规定,否则我不会做的。这似乎是一个愚蠢的想法/请求,但应该是偶然它是否可以做到的。
Either this is easyish to do, as specified, or I will not be doing it. It may seem a silly idea/request, but that should be incidental to whether it can be done.
认真上次。这里是展示我所期望发生的GIF动画。
Seriously last time. Here is an animated gif showing what I expect to happen.
在循环数据的每次迭代被添加到表中。这是明确的,因为我将能够传达它。
After each iteration of the loop data is added to the table. This is as clear as I am going to be able to communicate it.
推荐答案
OK,尝试4号:
N <- 5
set.seed(1)
x <- NULL
for (i in 1:N){
x <- rbind(x, table(rle(sample(0:1, 100000, replace=TRUE))))
}
x <- as.data.frame(x)
x$length <- as.numeric(rownames(x))
aggregate(x[, 1:2], list(x[[3]]), sum)
产地:
Group.1 0 1
1 1 62634 62531
2 2 31410 31577
3 3 15748 15488
4 4 7604 7876
5 5 3912 3845
6 6 1968 1951
7 7 979 971
8 8 498 477
9 9 227 246
10 10 109 128
11 11 65 59
12 12 24 30
13 13 21 11
14 14 7 10
15 15 0 4
16 16 4 2
17 17 0 1
18 18 0 1
如果你想在循环内聚集,做的:
If you want the aggregation inside the loop, do:
N <- 5
set.seed(1)
x <- NULL
for (i in 1:N){
x <- rbind(x, table(rle(sample(0:1, 100000, replace=TRUE))))
y <- aggregate(x, list(as.numeric(rownames(x))), sum)
print(y)
}
这篇关于从追加回路RLE结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!