0

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

0

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

Comments

2

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

5 Comments

Thanks. But this output does not give to when total result when I am increasing "id" size 50 and more.
@Rudo88 It is based on the input data and expected output. If your data is different, please do change in your post along with expected
Thanks. I want to know, is it general code if I extend the size of the data?
@Rudro88 Yes, it is as we are grouping by 'id'
Ok. Thanks again for cooperation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.