获取R中多个ID的id的不同行的日期差异

时间:2017-07-19 05:56:07

标签: r loops if-statement

如果id相同,我想获得连续2行的日期差异。

一个简单的代码是:

for(i in 2:nrow(Data))
{
  if(id[i]==id[i-1]
  {
   dated[i]-dated[i-1]
  }
}

但是这个循环需要很长时间才能执行。有没有更快的方法在超过200万行上运行这种代码?

1 个答案:

答案 0 :(得分:2)

我们可以使用/// Server.js var users = [] io.sockets.on('connect', (socket) => { socket.on("joingame", (jgCallback) => { if (users.length < 3) { if (users.length > 0) { users[users.length - 1].nextSocket = socket; // connect this socket to the previous one } users.push(socket); if (users.length === 3) { socket.nextSocket = users[0]; // connect this socket to the first one } jgCallback({ success: true }) } else { jgCallback({ success: false }) } }) socket.on("sendword", (payload, swCallback) => { if (users.length === 3 && socket.nextSocket) { // There are 3 users, and This socket has a "nextSocket" property socket.nextSocket.emit("sendword", payload); // send to the neighbor swCallback({success:true}) } else { swCallback({success:false}) } }) }); /// Clients.js io.emit("joingame", (jgCallbackResponse) => { if (jgCallbackResponse.success){ // you are in the game } })// Join the game. io.on("sendword", (payload) => { // from the previous user in the chain console.log(payload.word) }) io.emit("sendword", { word: "my word" }, (swCallbackResponse) => { if (swCallbackResponse.success){ // word sent } }); 执行此操作。将'data.frame'转换为'data.table'(data.table),按'id'分组,得到'date'列的差异(setDT(df1)

diff