Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (50.2k points)

Below is my data-frame:

enter image description here

I wanted to get the actual entry as follows:

enter image description here

1 Answer

0 votes
by (107k points)

In R, you can use diff() as a arugment in the transform() :

transform(df, entry = c(cum_entry[1], diff(cum_entry)))

#  Day cum_entry entry

#1   1         0     0

#2   2         1     1

#3   3         2     1

#4   3         2     0

#5   4         3     1

#6   4         3     0

In dplyr, we can use lag() as an argument in the mutate function :

library(dplyr)

df %>%  mutate(entry = cum_entry - lag(cum_entry, default = 0))

If you are a beginner and want to know more about R then do check out the R programming course that will help you out in a better way. 

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...