如果在R中满足条件,则将值从一列添加到新列

时间:2014-09-29 07:09:09

标签: r conditional-statements

我有一个如下所示的数据框(df)以及以下列

“由我解决”列的索引值为“TRUE / FALSE”。它说明票是否由我解决。

row  Tkt.number  Group   solved.by.me
1    Tkt478111  Group A  FALSE
2    Tkt478111  Group B  FALSE
3    Tkt478111  Group C  FALSE
4    Tkt478111  Group A  FALSE
5    Tkt478111  Group A  FALSE
6    Tkt478111  Group X  FALSE
7    Tkt478111  Group A  FALSE
8    Tkt478111  Group X  FALSE
9    Tkt478145  Group A  TRUE
10   Tkt478145  Group A  TRUE
11   Tkt478145  Group B  TRUE
12   Tkt478145  Group B  TRUE
13   Tkt478145  Group B  TRUE

我想检查以下条件并将结果存储在新列中 Moved.out.from

条件:

  1. 如果“由我解决”是假的
  2. 如果故障单从某个指定的组列表中移出(例如“grp_list” - 组A,B,C,D)并且没有返回上述组
  3. row Tkt       Group   solved.by.me  Moved.out.from
    1   Tkt478111 Group A FALSE   
    2   Tkt478111 Group B FALSE   
    3   Tkt478111 Group C FALSE   
    4   Tkt478111 Group A FALSE   
    5   Tkt478111 Group A FALSE   
    6   Tkt478111 Group X FALSE   
    7   Tkt478111 Group A FALSE          Group A
    8   Tkt478111 Group X FALSE   
    9   Tkt478145 Group A TRUE    
    10  Tkt478145 Group A TRUE    
    11  Tkt478145 Group B TRUE    
    12  Tkt478145 Group B TRUE    
    13  Tkt478145 Group B TRUE
    

    请注意第7行,其中“Tkt478111”从A组移出(可在我们的预定义列表中找到)但没有回来,但在第5行中不是这样。因为它从A组出来并且来了再次回来。 (组X在预定义的组列表中不可用)

    我是R的新手,不知道如何处理它。任何想法如何执行此操作?在此先感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

确定有人会使用简单的dplyr / data.table方法解决问题,但与此同时,base R的一种方式:

df <- read.table(text='row  Tkt  Group   solved.by.me
1    Tkt478111  A  FALSE
2    Tkt478111  B  FALSE
3    Tkt478111  C  FALSE
4    Tkt478111  A  FALSE
5    Tkt478111  A  FALSE
6    Tkt478111  X  FALSE
7    Tkt478111  A  FALSE
8    Tkt478111  X  FALSE
9    Tkt478145  A  TRUE
10   Tkt478145  A  TRUE
11   Tkt478145  B  TRUE
12   Tkt478145  B  TRUE
13   Tkt478145  B  TRUE', header=TRUE, stringsAsFactors=FALSE)

grps <- c('A', 'B', 'C', 'D')

newdf <- do.call(rbind, 
                 lapply(split(df, df$Tkt), function(x) {
                   i <- which(x$Group %in% grps & !x$solved.by.me)
                   x$moved.from <- NA
                   if (length(i) > 0 && tail(i, 1) != nrow(x)) {
                     x$moved.from[tail(i, 1)] <- x$Group[tail(i, 1)] 
                   }
                   x
                 }))

row.names(newdf) <- NULL

newdf

#    row       Tkt Group solved.by.me moved.from
# 1    1 Tkt478111     A        FALSE       <NA>
# 2    2 Tkt478111     B        FALSE       <NA>
# 3    3 Tkt478111     C        FALSE       <NA>
# 4    4 Tkt478111     A        FALSE       <NA>
# 5    5 Tkt478111     A        FALSE       <NA>
# 6    6 Tkt478111     X        FALSE       <NA>
# 7    7 Tkt478111     A        FALSE          A
# 8    8 Tkt478111     X        FALSE       <NA>
# 9    9 Tkt478145     A         TRUE       <NA>
# 10  10 Tkt478145     A         TRUE       <NA>
# 11  11 Tkt478145     B         TRUE       <NA>
# 12  12 Tkt478145     B         TRUE       <NA>
# 13  13 Tkt478145     B         TRUE       <NA>