简体   繁体   English

R为多个条件下的一组值创建一个新列

[英]R create a new column for set of values by multiple conditions

i am not sure if the title is precise enough to describe my problem. 我不确定标题的准确性是否足以描述我的问题。 I have a data frame with sets 1:n of two groups A and B and possible status Calm , Action1 and Action2 . 我有集的数据帧1:n两组AB和可能的状态CalmAction1Action2

  triggers <- c("Action1", "Action2") 
  df <- data.frame(Set    = c(rep(1, 4), rep(2, 4), rep(1, 4)),                                                                             
                   Group  = c(rep("A", 4), rep("A", 4), rep("B", 4)),                                                                     
                   Status = c(rep("Calm",3), "Action1", rep("Calm",3), 
                                "Action2", rep("Calm", 4))) 
Set Group Status
1    A    Calm
1    A    Calm
1    A    Calm
1    A    Action1
2    A    Calm
2    A    Calm
2    A    Calm
2    A    Action2
1    B    Calm
1    B    Calm
1    B    Calm
1    B    Calm

Based on the vector triggers i want to create a new column where the following condition is met. 基于矢量triggers我想创建一个满足以下条件的新列。

If in one set (for each group) an Action1 or Action2 occurs than write into a new column the status Action also for Status Calm . 如果在一组(每个组)的Action1Action2比写入到一个新的列中的状态发生Action也为Status Calm

The result in the new column should be c(rep("Action", 8), rep("Calm", 4)) . 新列中的结果应为c(rep("Action", 8), rep("Calm", 4))

I could not solve the problem with the multiple condition in this data frame. 我无法解决此数据帧中的多重条件问题。 Hope, someone can help me and forgives my non-mathematical description for the problem. 希望有人可以帮助我,并原谅我对这个问题的非数学描述。

This should work: 这应该工作:

df %>% dplyr::group_by(Group, Set) %>% 
  do(mutate(. ,result = ifelse(any(Status %in% triggers), "Action", "Calm")))

Source: local data frame [12 x 4]
Groups: var2, var1 [3]
    Set  Group    Status result
   <dbl> <chr>   <chr>  <chr>
1      1     A    Calm Action
2      1     A    Calm Action
3      1     A    Calm Action
4      1     A Action1 Action
5      2     A    Calm Action
6      2     A    Calm Action
7      2     A    Calm Action
8      2     A Action2 Action
9      1     B    Calm   Calm
10     1     B    Calm   Calm
11     1     B    Calm   Calm
12     1     B    Calm   Calm

Here is a data.table solution as requested 这是根据要求的data.table解决方案

library(data.table)
dt1 <- setDT(df)
dt1[, result := ifelse(sum(Status %in% triggers) == 0, "Calm", "Action"), by = .(Group, Set)]

With the data set provided this is roughly 6.5 times as fast as using dplyr 使用提供的数据集,这大约是使用dplyr 6.5倍

Considering dft as your inut dataframe, you could use dplyr and try: dft当作您的输入数据框,可以使用dplyr并尝试:

dft %>% 
  group_by(Group, Set) %>%
  mutate(nc = if_else( Status %in% triggers, 1, 0)) %>%
  mutate(nc = max(nc)) %>%
  mutate(nc2 = if_else(nc == 1, "Action", as.character(Status))) %>%
  select(nc2)

ps the second and third mutate commands could be merged into one. ps第二个和第三个mutate命令可以合并为一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM