通过dplyr在组内应用rep()(Applying rep() within groups through dplyr)
我一直在尝试在组内复制1和2的二进制输出。 我想使用
rep
和dplyr
,但我似乎无法理解如何在组内应用rep
。 我已经能够通过手动分离分组并指定每组的正确范围来实现。 我想知道如何使用dplyr
应用rep
。这是一个示例数据。
df <- data.frame(date = c("2017-01-01", "2017-01-01", "2017-01-01", "2017-01-01", "2017-01-01", "2017-01-01", "2017-01-01", "2017-01-02", "2017-01-02", "2017-01-02", "2017-01-02", "2017-01-02", "2017-01-02", "2017-01-02", "2017-01-02", "2017-01-02", "2017-01-02", "2017-01-02"), loc =c("AB", "AB", "AB", "AB", "AB", "AB", "AB", "AB", "CD", "CD", "CD", "CD", "CD", "CD", "CD", "CD", "CD", "CD"), cat = c("a", "a", "a", "b", "b", "b", "b", "b", "c", "c", "c", "c", "c", "d", "d", "d", "d", "d"))
这基本上是我在整个数据集上应用的每个分组运行的代码。
df$type <- rep(1:2,nrow(df)/2)
如您所见,输出忽略了列
cat
。cat b & d
应该从1开始。date loc cat type 1 2017-01-01 AB a 1 2 2017-01-01 AB a 2 3 2017-01-01 AB a 1 4 2017-01-01 AB b 2 5 2017-01-01 AB b 1 6 2017-01-01 AB b 2 7 2017-01-01 AB b 1 8 2017-01-02 AB b 2 9 2017-01-02 CD c 1 10 2017-01-02 CD c 2 11 2017-01-02 CD c 1 12 2017-01-02 CD c 2 13 2017-01-02 CD c 1 14 2017-01-02 CD d 2 15 2017-01-02 CD d 1 16 2017-01-02 CD d 2 17 2017-01-02 CD d 1
更新:这是所需的输出。
date loc cat type 1 2017-01-01 AB a 1 2 2017-01-01 AB a 2 3 2017-01-01 AB a 1 4 2017-01-01 AB b 1 5 2017-01-01 AB b 2 6 2017-01-01 AB b 1 7 2017-01-01 AB b 2 8 2017-01-02 AB b 1 9 2017-01-02 CD c 1 10 2017-01-02 CD c 2 11 2017-01-02 CD c 1 12 2017-01-02 CD c 2 13 2017-01-02 CD c 1 14 2017-01-02 CD d 1 15 2017-01-02 CD d 2 16 2017-01-02 CD d 1 17 2017-01-02 CD d 2
I've been trying to replicate a binary output of 1 and 2 within groups. I'd like to make use of
rep
anddplyr
, but I can't seem to understand how to applyrep
within groups. I've been able to do it by manually separating the groupings and specifying the correct range per group. I would like to know howrep
could be applied usingdplyr
.Here's a sample data.
df <- data.frame(date = c("2017-01-01", "2017-01-01", "2017-01-01", "2017-01-01", "2017-01-01", "2017-01-01", "2017-01-01", "2017-01-02", "2017-01-02", "2017-01-02", "2017-01-02", "2017-01-02", "2017-01-02", "2017-01-02", "2017-01-02", "2017-01-02", "2017-01-02", "2017-01-02"), loc =c("AB", "AB", "AB", "AB", "AB", "AB", "AB", "AB", "CD", "CD", "CD", "CD", "CD", "CD", "CD", "CD", "CD", "CD"), cat = c("a", "a", "a", "b", "b", "b", "b", "b", "c", "c", "c", "c", "c", "d", "d", "d", "d", "d"))
This is basically the code I run per grouping applied on the entire dataset.
df$type <- rep(1:2,nrow(df)/2)
As you can see, the output disregards the column
cat
.cat b & d
should have started at 1.date loc cat type 1 2017-01-01 AB a 1 2 2017-01-01 AB a 2 3 2017-01-01 AB a 1 4 2017-01-01 AB b 2 5 2017-01-01 AB b 1 6 2017-01-01 AB b 2 7 2017-01-01 AB b 1 8 2017-01-02 AB b 2 9 2017-01-02 CD c 1 10 2017-01-02 CD c 2 11 2017-01-02 CD c 1 12 2017-01-02 CD c 2 13 2017-01-02 CD c 1 14 2017-01-02 CD d 2 15 2017-01-02 CD d 1 16 2017-01-02 CD d 2 17 2017-01-02 CD d 1
UPDATE: Here's the desired output.
date loc cat type 1 2017-01-01 AB a 1 2 2017-01-01 AB a 2 3 2017-01-01 AB a 1 4 2017-01-01 AB b 1 5 2017-01-01 AB b 2 6 2017-01-01 AB b 1 7 2017-01-01 AB b 2 8 2017-01-02 AB b 1 9 2017-01-02 CD c 1 10 2017-01-02 CD c 2 11 2017-01-02 CD c 1 12 2017-01-02 CD c 2 13 2017-01-02 CD c 1 14 2017-01-02 CD d 1 15 2017-01-02 CD d 2 16 2017-01-02 CD d 1 17 2017-01-02 CD d 2
原文:https://stackoverflow.com/questions/43462154
最满意答案
在当前的实现中,最快的方法是检查是否添加了
pause
类,如果是,则跳过动画:if($this.classList.contains('pause'))continue;
但是,由于所有current
项都是在slideShows
每次迭代中使用document.querySelectorAll('.current')
处理的,因此您可能希望删除内部循环并使用$this.querySelector('.current')
来获取当前项目具体的幻灯片放映:var slideshows = document.querySelectorAll('.slideshow'); function startSlideshow() { return window.setInterval(function(){ for (var i=0;i<slideshows.length;i++){ var $this = slideshows[i]; if($this.classList.contains('pause'))continue; var e = $this.querySelector('.current'); (e.nextElementSibling || $this.firstElementChild).classList.add('current'); e.classList.remove('current'); } }, 1000); } var id = startSlideshow(); for (var i=0;i<slideshows.length;i++){ slideshows[i].addEventListener('click', function(){ this.classList.toggle('pause'); }); }
另一个选择是让每个幻灯片包含自己的计时器,并启动/停止它。 但是,这可能会导致它们在停止/启动后彼此不同步。
顺便说一句,您可以使用对象,设置dataSet属性或从
slideshows
阵列中添加/删除div,而不是添加暂停类。In your current implementation, the quickest way is to check if the
pause
class is added and skip the animation if it is:if($this.classList.contains('pause'))continue;
However, since allcurrent
items are handled withdocument.querySelectorAll('.current')
inside each iteration ofslideShows
, you might want to remove the inner loop and use$this.querySelector('.current')
to get the current item of that specific slide show:var slideshows = document.querySelectorAll('.slideshow'); function startSlideshow() { return window.setInterval(function(){ for (var i=0;i<slideshows.length;i++){ var $this = slideshows[i]; if($this.classList.contains('pause'))continue; var e = $this.querySelector('.current'); (e.nextElementSibling || $this.firstElementChild).classList.add('current'); e.classList.remove('current'); } }, 1000); } var id = startSlideshow(); for (var i=0;i<slideshows.length;i++){ slideshows[i].addEventListener('click', function(){ this.classList.toggle('pause'); }); }
Another option is to have each slideshow contain its own timer, and start/stop that. However that could cause them to run out of sync with each other after a stop/start.
As an aside, instead of adding a pause class, you could also use objects, set a dataSet property or have the div add/remove itself from the
slideshows
array
相关问答
更多-
不可阻挡的计时器(Unstoppable Timer)[2023-07-07]
您必须重构您的代码以使其工作,System.Timers.Timer使用ThreadPool来运行回调例程。 class Program { public static int t = 2; static System.Timers.Timer aTimer = new System.Timers.Timer(); public static void Main() { // Hook up the Elapsed event for the timer. ... -
node.js:如何使用setInterval和clearInterval?(node.js: how to use setInterval and clearInterval?)[2024-04-26]
使用setInterval() 如果您需要以指定的时间间隔重复执行代码块,该怎么办? 为此,Node具有称为setInterval()和clearInterval()的方法。 setInterval()函数非常像setTimeout(),它使用相同的参数(例如回调函数,延迟和用于传递给回调函数的任何可选参数)。 下面显示一个简单的setInterval()例子: var interval = setInterval(function(str1, str2) { console.log(str1 + " ... -
onResume是罪魁祸首。 我已经成功地实现了onResume。 onResume was the culprit. I have since implemented onResume with success.
-
尝试这个: FXMLLoader loader = new FXMLLoader(); loader.setLocation(getClass().getResource("/ooppFinalProject/view/HomePage1.fxml")); myScene = (Scene) ((Node) event.getSource()).getScene(); Stage stage = (Stage) (myScene).getWindow(); Parent nextView = loader. ...
-
除了我以前的评论 - 这里有一些伪代码: enum GameState { Starting, CreateBrick, BrickFalling, MoveSideways } private GameState curState = Starting; private ushort ctSideways; private void TimerBrick_Tick(object sender, EventArgs e) { switch (curState) ...
-
设置节点的计时器(setup timer for a node)[2023-08-02]
你应该使用场景的update()函数。 该覆盖运行每帧,以便您可以检查您的定位,并跟踪每个节点,您可以启动或重新启动一个wait()操作,在完成时删除该节点。 就像是, if ball.position.x == 200 && ball.position.y == 200 { let wait = SKAction.wait(forDuration: 5.0) let remove = SKAction.removeFromParent() let sequence = SKActi ... -
在android中启动计时器(Starting a timer in android)[2023-08-20]
最简单的方法:使用System.currentTimeMillis(),所以: // Here I start the timer. t0=System.currentTimeMillis(); "while loop" { t=System.currentTimeMillis()-t0; // There you have the elapsed time If (t==10) then bla If (t==15) then blabla If (t==20) then blablabla (... ... -
多人倒数计时器(Multiplayer Countdown Timer)[2022-04-02]
您可以使用cookie或会话来保持时间。 这样,当用户刷新时,他们将不会重置计数。 Javascript每次都会重置计数,因为它们中的每一个都被视为新请求。 使用cookie或会话。 You can use cookies or session to keep the time. This way when the user refreshes, they will not be resetting the count. Javascript will reset the count each time b ... -
一个节点上的clearInterval,而另一个节点上的计时器继续(clearInterval on one node while timer continues on another)[2022-11-29]
在当前的实现中,最快的方法是检查是否添加了pause类,如果是,则跳过动画: if($this.classList.contains('pause'))continue; 但是,由于所有current项都是在slideShows每次迭代中使用document.querySelectorAll('.current')处理的,因此您可能希望删除内部循环并使用$this.querySelector('.current')来获取当前项目具体的幻灯片放映: var slideshows = document.quer ... -
Javascript clearInterval不适用于计时器[重复](Javascript clearInterval not working for timer [duplicate])[2022-11-20]
这是因为你在这里覆盖了你的初始timer变量: document.getElementById("start").onclick = function () { // this clobbers your previous `timer` assignment var timer = setInterval(function(){ Ftimer() }, 1000); } 因此,只需删除var并使用外部作用域timer变量即可修复它: document.getElementById("s ...