Rudro88
Rudro88

Reputation: 231

Conditional variable using R code

I have a data set named "dats".

 id y  i j
  1 0  1 1
  1 0  1 2
  1 0  1 3 
  2 1  2 1
  2 1  2 2
  2 1  2 3

I want to calculate, a new variable ynew=(yij-1*yij) based on (y11*y12, y12*y13....so on). I have tried in this way:

ynew <- NULL

for(p in 1)
    {
    for (q in ni)
        {
        ynew[p,q] <- dats$y[dats$i==p & dats$j==q-1]*dats$y[dats$i==p & dats$j==q]
        }
    }

ynew

But it showing error!

Expected output

 id y  i j  ynew
  1 0  1 1  NA
  1 0  1 2  0
  1 0  1 3  0
  2 1  2 1  NA
  2 1  2 2  1
  2 1  2 3  1

Could anybody help? TIA

Upvotes: 0

Views: 63

Answers (2)

akrun
akrun

Reputation: 887118

May be we need to just multiply with the lag of 'y' grouped by 'id'

library(data.table)
setDT(dats)[, ynew := y * shift(y), by = id]
dats
#   id y i j ynew
#1:  1 0 1 1   NA
#2:  1 0 1 2    0
#3:  1 0 1 3    0
#4:  2 1 2 1   NA
#5:  2 1 2 2    1
#6:  2 1 2 3    1

It could also be done with roll_prod

library(RcppRoll)
setDT(dats)[, ynew := c(NA, roll_prod(y, 2)), by = id]
dats
#    id y i j ynew
#1:  1 0 1 1   NA
#2:  1 0 1 2    0
#3:  1 0 1 3    0
#4:  2 1 2 1   NA
#5:  2 1 2 2    1
#6:  2 1 2 3    1

Upvotes: 2

Sotos
Sotos

Reputation: 51592

Using dplyr and rollapply from zoo package,

library(dplyr)
library(zoo)

dats %>% 
   group_by(id) %>% 
   mutate(ynew = c(NA, rollapply(y, 1, by = 2, prod)))

#Source: local data frame [6 x 5]
#Groups: id [2]

#     id     y     i     j  ynew
#  (int) (int) (int) (int) (dbl)
#1     1     0     1     1    NA
#2     1     0     1     2     0
#3     1     0     1     3     0
#4     2     1     2     1    NA
#5     2     1     2     2     1
#6     2     1     2     3     1

Upvotes: 2

Related Questions