如何解决R-根据多个条件和事件时间创建新列
我需要根据多个条件和前几列的时间点来创建新列。我有以下数据框:
table <- data.frame(RowID=c("A1","A2","A3","A4","A5","A6","A7","A8","A9","A10","A11","A12","A13","A14","A15"),Machine=c("Ace","Ace","Ame","Cay","Gap","Dex","Dex"),Time=c(1,2,3,1,4,5,3),Status=c("Good","Good","Bad","Good"))
print(table)
RowID Machine Time Status
1 A1 Ace 1 Good
2 A2 Ace 2 Good
3 A3 Ace 3 Bad
4 A4 Ame 1 Bad
5 A5 Ame 2 Good
6 A6 Cay 1 Good
7 A7 Cay 2 Bad
8 A8 Cay 3 Good
9 A9 Cay 4 Good
10 A10 Cay 5 Bad
11 A11 Gap 1 Good
12 A12 Gap 2 Good
13 A13 Dex 1 Bad
14 A14 Dex 2 Bad
15 A15 Dex 3 Good
对于每一个机器,时间都会显示获取读数的时间。我想创建两个新列判决和结果。对于判决列,对于任何状态为“良好”的计算机,如果要在其“不良”(例如Ace和Cay)之前标记为“是”,否则,应将其标记为“否”。对于结果列,我想在机器首次出现“不良”状态时将其标记为“事件”,在出现“不良”状态之前将其标记为“良好”状态的“ BeforeEvent”。对于不在“不良”之前的任何其他“良好”状态,请标记为“之前”,对于在第一个“不良”状态之后的任何状态,标记为“之后”。
我希望获得的最终数据帧如下:
table_new <- data.frame(RowID=c("A1","Good"),Verdict=c("YES","YES","NO","NO"),Outcome=c("Before","BeforeEvent","Event","None","After","None"))
print(table_new)
RowID Machine Time Status Verdict Outcome
1 A1 Ace 1 Good YES Before
2 A2 Ace 2 Good YES BeforeEvent
3 A3 Ace 3 Bad YES Event
4 A4 Ame 1 Bad NO None
5 A5 Ame 2 Good NO None
6 A6 Cay 1 Good YES BeforeEvent
7 A7 Cay 2 Bad YES Event
8 A8 Cay 3 Good YES After
9 A9 Cay 4 Good YES After
10 A10 Cay 5 Bad YES After
11 A11 Gap 1 Good NO None
12 A12 Gap 2 Good NO None
13 A13 Dex 1 Bad NO None
14 A14 Dex 2 Bad NO None
15 A15 Dex 3 Good NO None
非常感谢您提供任何帮助,因为我将需要重复多次,因此如果可以自动执行将非常有用-谢谢!
解决方法
这里是一个示例(使用my_table
)。尚不清楚您是否可能经历了从好到坏的多次转换,或者可能如何处理。
首先,group_by
机器。我会考虑一个事件,其中上一行为“良好”,当前行为“不良”。布尔值可用于在发生这种情况时进行标记。
如果事件中组中的任何值为TRUE
,则裁决将标记为“是”,否则为“否”。
使用case_when
,您可以比较计算机组中的行号和事件首次发生的时间(如果组中有多个转换,则使用min
来指示结果)。
我希望这对您有帮助。
library(dplyr)
my_table %>%
group_by(Machine) %>%
mutate(Event = ifelse(lag(Status,default = "Bad") == "Good" & Status == "Bad",TRUE,FALSE),Verdict = ifelse(any(Event),"YES","NO"),Outcome = ifelse(Verdict == "NO","None",case_when(
row_number() + 1 < min(which(Event)) ~ "Before",row_number() < min(which(Event)) ~ "BeforeEvent",row_number() == min(which(Event)) ~ "Event",row_number() > min(which(Event)) ~ "After"
)))
输出
RowID Machine Time Status Event Verdict Outcome
<chr> <chr> <dbl> <chr> <lgl> <chr> <chr>
1 A1 Ace 1 Good FALSE YES Before
2 A2 Ace 2 Good FALSE YES BeforeEvent
3 A3 Ace 3 Bad TRUE YES Event
4 A4 Ame 1 Bad FALSE NO None
5 A5 Ame 2 Good FALSE NO None
6 A6 Cay 1 Good FALSE YES BeforeEvent
7 A7 Cay 2 Bad TRUE YES Event
8 A8 Cay 3 Good FALSE YES After
9 A9 Cay 4 Good FALSE YES After
10 A10 Cay 5 Bad TRUE YES After
11 A11 Gap 1 Good FALSE NO None
12 A12 Gap 2 Good FALSE NO None
13 A13 Dex 1 Bad FALSE NO None
14 A14 Dex 2 Bad FALSE NO None
15 A15 Dex 3 Good FALSE NO None
数据
my_table <- structure(list(RowID = c("A1","A2","A3","A4","A5","A6","A7","A8","A9","A10","A11","A12","A13","A14","A15"),Machine = c("Ace","Ace","Ame","Cay","Gap","Dex","Dex"),Time = c(1,2,3,1,4,5,3),Status = c("Good","Good","Bad","Good")),class = "data.frame",row.names = c(NA,-15L))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。