编程R ifelse条件循环

时间:2018-12-04 09:06:15

标签: r if-statement conditional-statements

你好,我需要编程R的帮助。我有data.frame B,有四列

x<- c(1,2,1,2,1,2,1,2,1,2,1,2,.......etc.)
y<-c(5,5,8,8,12,12,19,19,30,30,50,50,...etc.)
z<- c(2018-11-08,2018-11-08,2018-11-09,2018-11-09,2018-11-11,2018-11-11,2018-11-20,2018-11-20,2018-11-29,2018-11-29,2018-11-30,2018-11-30,.......etc.)
m<-c(0,1,1,0,1,1,0,1,0,1,0,1,...etc.)

200万行,我需要创建下一列。接下来的列应为

t<-c(0,1,0,0,0,0,0,1,0,1,0,1,....)

循环代码类似于

B$t[1]=ifelse(B$y[i]==B$y[i+1] & B$z[i]==B$z[i+1] & B$x[i]==2 & B$m[1]==1,1,0)
for (i in 2:length(B$z))
{
  B$t[i]<-ifelse(B$y[i]==B$y[i-1] & B$z[i]==B$z[i-1] & B$x[i]==2 & B$m[i]==1 & B$m[i]!=B$m[i-1],1,0)
}

我不想使用循环循环。

我在R中使用基本软件包。

当我有data.frame E时,我有一个新问题

x<- c(1,2,3,1,2,3,1,2,3,1,2,3,.......etc.)
    y<-c(5,5,5,8,8,8,12,12,12,,19,19,19,30,30,30,50,50,50,...etc.)
    z<- c(2018-11-08,2018-11-08,2018-11-08,2018-11-09,2018-11-09,2018-11-09,2018-11-11,2018-11-11,2018-11-11,2018-11-20,2018-11-20,2018-11-20,2018-11-29,2018-11-29,2018-11-29,2018-11-30,2018-11-30,2018-11-30,.......etc.)
    m<-c(0,1,1,0,0,1,0,1,0,1,0,1,0,0,1...etc.)

200万行,我需要创建下一列。接下来的列应为

    t<-c(0,1,0,0,1,....)

循环代码类似于

E$t[1]=ifelse(E$y[i]==E$y[i+1] & E$z[i]==E$z[i+1] & E$x[1]==2 & E$m[1]==1,1,0)
E$t[2]=ifelse(E$y[i]==E$y[i+1] & E$z[i]==E$z[i+1] & E$x[2]==3 & E$m[2]==1,1,0)
for (i in 3:length(E$y))
{
  E$t[i]<-ifelse(E$y[i]==E$y[i-2] & E$z[i]==E$z[i-2] & E$x[i]==3 & E$m[i]==1 &
                     E$m[i-1]==0 & E$m[i-2]==0,1,0)
}

我不想使用循环循环。

我在R中使用基本软件包。

2 个答案:

答案 0 :(得分:1)

通过dplyr,您可以使用if_elselag

library(dplyr)
dat %>% 
  mutate(t = if_else(
    y == lag(y) & z == lag(z) & x == 2 & m == 1 & m != lag(m), 1, 0)
         ) # mutate lets you create a new variable in dat (named t here)

#    x  y          z m t
# 1  1  5 2018-11-08 0 0
# 2  2  5 2018-11-08 1 1
# 3  1  8 2018-11-09 1 0
# 4  2  8 2018-11-09 0 0
# 5  1 12 2018-11-11 1 0
# 6  2 12 2018-11-11 1 0
# 7  1 19 2018-11-20 0 0
# 8  2 19 2018-11-20 1 1
# 9  1 30 2018-11-29 0 0
# 10 2 30 2018-11-29 1 1
# 11 1 50 2018-11-30 0 0
# 12 2 50 2018-11-30 1 1

数据:

x<- c(1,2,1,2,1,2,1,2,1,2,1,2)
y<-c(5,5,8,8,12,12,19,19,30,30,50,50)
z<- c("2018-11-08","2018-11-08","2018-11-09","2018-11-09","2018-11-11","2018-11-11","2018-11-20","2018-11-20","2018-11-29","2018-11-29","2018-11-30","2018-11-30")
m<-c(0,1,1,0,1,1,0,1,0,1,0,1)

dat <- data.frame(x, y, z, m)

答案 1 :(得分:1)

以下是基于R的解决方案:

N <- nrow(B)
B$t <- ifelse(B$y==c(NA, B$y[-N]) & B$z==c(NA, B$z[-N]) & B$x==2 & B$m==1 & B$m!=c(NA, B$m[-N]), 1, 0)

这是data.table的解决方案:

library("data.table")
B <- data.table(
x= c(1,2,1,2,1,2,1,2,1,2,1,2), y= c(5,5,8,8,12,12,19,19,30,30,50,50),
z= c("2018-11-08", "2018-11-08", "2018-11-09", "2018-11-09", "2018-11-11", "2018-11-11", "2018-11-20",
     "2018-11-20", "2018-11-29", "2018-11-29", "2018-11-30", "2018-11-30"),
m= c(0,1,1,0,1,1,0,1,0,1,0,1)
)
B[, t := ifelse(y==c(NA, y[- .N]) & z==c(NA, z[- .N]) & x==2 & m==1 & m!=c(NA, m[- .N]), 1, 0)]

或(如果可以接受逻辑

B[, t := (y==c(NA, y[- .N]) & z==c(NA, z[- .N]) & x==2 & m==1 & m!=c(NA, m[- .N]))]

或使用shift()

B[, t := (y==shift(y) & z==shift(z) & x==2 & m==1 & m!=shift(m))]