在R中按每组变异 [英] mutate per group by in R

查看:54
本文介绍了在R中按每组变异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从传感器数据中识别出零件,并给它们一个ID.因此,我想按传感器"列对以下数据集进行分组,并查看值"行是否从0切换为1.确定第一行时,caseid切换为1(如手工列caseid一样).只要值保持为1,它就保持为1.当它变为0时,应切换回0.在下一次从0切换到1的情况下,caseid应该变为2,因为第二部分被传感器识别,依此类推.

i want to identify pieces from sensor data and give them an ID. Therefore I want to group the following dataset by Sensor column and look whether the Value row switched from 0 to 1. When it does the first piece is identified and the caseid switches to 1 (as in the handmade column caseid). It remains 1 as long the value stays 1. When it becomes 0 it should switche back to 0. At the next switch from 0 to 1 the caseid should become 2 because the second piece is recognized by the sensor and so forth..

time = c("07:00:01","07:00:01","07:00:01","07:00:02","07:00:02","07:00:02","07:00:03","07:00:03","07:00:03","07:00:04",
     "07:00:04","07:00:04","07:00:05","07:00:05","07:00:05","07:00:06","07:00:06","07:00:06","07:00:07","07:00:07",
     "07:00:07","07:00:08","07:00:08","07:00:08","07:00:09","07:00:09","07:00:09")
sensor = c(10001,10002,10003,10001,10002,10003,10001,10002,10003,10001,10002,10003,10001,10002,10003,10001,10002,10003,
       10001,10002,10003,10001,10002,10003,10001,10002,10003)
values = c(0,0,0,1,0,0,1,0,0,0,1,0,0,1,0,0,1,0,0,0,0,1,0,1,1,0,1)
caseid = c(0,0,0,1,0,0,1,0,0,0,1,0,0,1,0,0,1,0,0,0,0,2,0,1,2,0,1)

data = data.frame(time,sensor,values,caseid)

(所以data $ caseid是我想要得到的)

(So the data$caseid is what I am trying to get)

我认为可以通过某种方式来实现这一目标,但是我做得不好,因此我选择了另一种(草率的)方法.那就是我得到的.

I think this can be achieved somehow by a group by but I couldn't get it right so I choose another (sloppy) approach. Thats what i got.

data%>% 
filter(Sensor=="10002") -> sensor_data_temp

sensor_data_temp$CaseID2 <- NA 
case_id = 1

for(i in 1:nrow(sensor_data_temp)){

   current_value <- sensor_data_temp[i,"values"]
   next_value <- sensor_data_temp[i+1,"values"]

   if(i+1 > nrow(sensor_data_temp)){
     break
   }

   if(current_value==0 & next_value==1 || current_value==1 & next_value==1){
     sensor_data_temp$CaseID2[i+1] <- case_id
   }
   else if(current_value==1 & next_value==0){
     sensor_data_temp$CaseID2[i+1] <- 0
     case_id = case_id +1
   }
   else{
     sensor_data_temp$CaseID2[i+1] <- 0
   }

}

我认为这就是我如何获取一个传感器的Caseid的方法.但是我不知道如何将每个传感器都放入一个数据帧中(如上一个)

I think thats how I could get the caseid's for one sensor. But I have no idea how I can manage to get every sensor into one dataframe (as the one above)

我敢肯定,有一种更优雅的方式来获得我想要的东西.

I am sure there is a much more elegant way to get what I want.

我希望有人能帮助我..在此先感谢!:)

I hope somebody can help me.. Thanks in advance! :)

推荐答案

这是一种方法:

library(dplyr)

mutate(group_by(arrange(data, sensor, time), sensor),
       caseID = case_when(values != 0 ~ cumsum(diff(c(0, values)) > 0),
                          TRUE ~ 0L))

这篇关于在R中按每组变异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆