简体   繁体   English

用R编程中的条件循环

[英]Loop with conditions in R programming

I would like to compare the previous row value whether it is same as the current one (for more than 1 variables and also using list of values). 我想比较前一行的值是否与当前行的值相同(对于多个变量,还使用值列表)。 In this case how do I perform write code. 在这种情况下,如何执行编写代码。 I read 'apply' functions can be used. 我阅读了“应用”功能,可以使用。

I searched this topic here before posting this question found somewhat similar but unable to find the exact one. 在发布此问题之前,我在这里搜索了此主题,发现该问题有些相似,但找不到确切的问题。 I'm quite new to R. 我对R很陌生。

Here is my sample table: (Flag needs to be done based on conditions) 这是我的示例表:(需要根据条件完成标记)

Ticket No   V1  V2  Flag
Tkt10256    1   X   0
Tkt10257    1   aa  0
Tkt10257    2   bb  1
Tkt10257    3   x   0
Tkt10260    1   cc  0
Tkt10260    2   aa  1
Tkt10262    3   bb  0

I have to Flag based on the below conditions (if all the conditions are satisfied then mark as 1) 我必须根据以下条件进行标记 (如果满足所有条件,则标记为1)

  1. Variable 2 should be the following one of 4 names (aa, bb, cc, dd) 变量2应该是以下4个名称之一(aa,bb,cc,dd)
  2. Variable 1 should be the different from previous row 变量1应与上一行不同
  3. Ticket number has to be the same as previous row 机票编号必须与上一行相同

Thanks in advance for the help ! 先谢谢您的帮助 !

An approach without looping: 没有循环的方法:

indx1 <- with(df, V2 %in% paste0(letters[1:4], letters[1:4]) )
indx2 <- with(df, c(TRUE,V1[-1]!=V1[-length(V1)]))
indx3 <- with(df, c(FALSE,Ticket.No[-1]==Ticket.No[-nrow(df)]))

df$Flag <- (indx1 & indx2 & indx3)+0
df$Flag
#[1] 0 0 1 0 0 1 0

data 数据

df <- structure(list(Ticket.No = c("Tkt10256", "Tkt10257", "Tkt10257", 
"Tkt10257", "Tkt10260", "Tkt10260", "Tkt10262"), V1 = c(1L, 1L, 
2L, 3L, 1L, 2L, 3L), V2 = c("X", "aa", "bb", "x", "cc", "aa", 
"bb"), Flag = c(0L, 0L, 1L, 1L, 0L, 1L, 0L)), .Names = c("Ticket.No", 
"V1", "V2", "Flag"), class = "data.frame", row.names = c(NA, 
-7L))

One more: 多一个:

Check this on your larger data. 在更大的数据上进行检查。 I'm not exactly sure if duplicated is the right function to use there. 我不确定是否可以在其中使用duplicated功能。 If the numbers in the TicketNo column are increasing (ie the Xs in TktXXXXX), then it should work fine. 如果TicketNo列中的数字增加(即TktXXXXX中的X),则应该可以正常工作。

> dat2 <- dat[dat$V2 %in% c("aa", "bb", "cc", "dd"),]
> rn <- rownames(dat2)[duplicated(dat2[[1]]) & !c(FALSE, diff(dat2[[2]]) == 0)]
> dat$Flag <- (rownames(dat) %in% rn)+0
> dat
#   TicketNo V1 V2 Flag
# 1 Tkt10256  1  X    0
# 2 Tkt10257  1 aa    0
# 3 Tkt10257  2 bb    1
# 4 Tkt10257  3  x    0
# 5 Tkt10260  1 cc    0
# 6 Tkt10260  2 aa    1
# 7 Tkt10262  3 bb    0

A variation on @Akrun's answer: @Akrun答案的变化形式:

with(df, 
  V2 %in% c("aa","bb","cc","dd") &  
  c(FALSE,diff(V1) != 0) &
  c(FALSE,head(Ticket.No, -1)) == Ticket.No
) + 0

#[1] 0 0 1 0 0 1 0

Try: 尝试:

for(i in 2:nrow(ddf)){
   ddf$Flag[i] = ifelse(  ddf$V2[i] %in% c('aa', 'bb', 'cc', 'dd') 
           && ddf$V1[i] != ddf$V1[(i-1)] 
           &&  ddf$TicketNo[i] == ddf$TicketNo[(i-1)]
         ,1,0)
 }
ddf
  TicketNo V1 V2 Flag
1 Tkt10256  1  X    0
2 Tkt10257  1 aa    0
3 Tkt10257  2 bb    1
4 Tkt10257  3  x    0
5 Tkt10260  1 cc    0
6 Tkt10260  2 aa    1
7 Tkt10262  3 bb    0

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

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