条件更改或不更改组中的所有变量

时间:2016-04-20 07:45:29

标签: r dplyr

根据是否满足某些条件,我可能会在clas中更改名为df的变量。如果不满足条件,不要改变任何东西。

V <- seq(10,50,10)
gr = rep(seq(1,2),each=5)
clas=c("Normal","Abnormal","Fair","Fair","Normal","Fair","Normal","Normal","Abnormal","Fair")

df <- data.frame(V,gr,clas)

   #      V gr     clas
   #  1  10  1   Normal
   #  2  20  1 Abnormal
   #  3  30  1     Fair
   #  4  40  1     Fair
   #  5  50  1   Normal
   #  6  10  2     Fair
   #  7  20  2   Normal
   #  8  30  2   Normal
   #  9  40  2 Abnormal
   #  10 50  2     Fair

如果在clasNormal每个组中显示Normal,我想将V=20列设置为所有V=30值。如果这种情况不满意,则无需更改该组。

预期输出

df_modif
   #      V  gr    clas
   #  1  10  1   Normal
   #  2  20  1 Abnormal
   #  3  30  1     Fair
   #  4  40  1     Fair
   #  5  50  1   Normal
   #  6  10  2   Normal
   #  7  20  2   Normal
   #  8  30  2   Normal
   #  9  40  2   Normal
   #  10 50  2   Normal

2 个答案:

答案 0 :(得分:5)

基于OP的描述

library(dplyr)
df %>%
    group_by(gr) %>% 
    mutate(clas = as.character(clas),
           clas = if(any(clas =="Normal" & V ==20) & 
                     any(clas=="Normal" & V==30)) "Normal"
                 else clas)
#       V    gr     clas
#   (dbl) (int)    (chr)
#1     10     1   Normal
#2     20     1 Abnormal
#3     30     1     Fair
#4     40     1     Fair
#5     50     1   Normal
#6     10     2   Normal
#7     20     2   Normal
#8     30     2   Normal
#9     40     2   Normal
#10    50     2   Normal

同样可以在data.table

中实施
library(data.table)
setDT(df)[df[,  .I[any(clas == "Normal" & V == 20) &
     any(clas == "Normal" & V ==30)], gr]$V1, clas := factor("Normal")]

答案 1 :(得分:1)

选择要更改的值,然后覆盖它们

df$clas[df$V %in% c(20, 30)] <- "Normal"