Error saying Error in lag x 1 unused argument 1

0 votes

I need to refer to lagged and lead values of variables but I cannot seem to get the code for the lag function working as documented.

> library(dplyr)
> x <- seq(100,500,100)
> x
[1] 100 200 300 400 500
> lead(x,1)
[1] 200 300 400 500  NA
> lag(x,1)
Error in lag(x, 1) : unused argument (1)
> 
Nov 16, 2018 in Data Analytics by Ali
• 11,360 points
1,693 views

1 answer to this question.

0 votes

lag only works as expected with time-series objects. For instance:

cbind(x = ts(1:10), y = lag(1:10, -1))

In your case:

lag_n <- function(x, n) {c(rep(NA, n), x)[1:length(x)])
answered Nov 16, 2018 by Maverick
• 10,840 points

Related Questions In Data Analytics

0 votes
1 answer

Error saying "Error in xy.coords(x, y, xlabel, ylabel, log) : 'x' and 'y' lengths differ"

Make sure is a factor time <- as.factor(c("pm", ...READ MORE

answered Nov 9, 2018 in Data Analytics by Maverick
• 10,840 points
7,455 views
0 votes
1 answer

Error saying "Error in x$children[[1]] : subscript out of bounds" while web scrapping

You could try the httr library: library(XML) library(httr) url <- 'http://www.sainsburys.co.uk/shop/gb/groceries/fruit-veg/all-fruit#langId=44&storeId=10151&catalogId=10122&categoryId=12545&parent_category_rn=12518&top_category=12518&pageSize=30&orderBy=FAVOURITES_FIRST&searchTerm' doc <- ...READ MORE

answered Nov 9, 2018 in Data Analytics by Maverick
• 10,840 points
2,211 views
+1 vote
1 answer

Error saying " ERROR: unused argument (InformationCriterion = InformationCriterion[j]) "

Brief invoke REBMIX as : REBMIX[[i, j, k]] ...READ MORE

answered Nov 9, 2018 in Data Analytics by Maverick
• 10,840 points
642 views
+1 vote
1 answer
+10 votes
3 answers

Which is a better initiative to learn data science: Python or R?

Well it truly depends on your requirement, If ...READ MORE

answered Aug 9, 2018 in Data Analytics by Abhi
• 3,720 points
1,627 views
+1 vote
1 answer

Error saying "vector size cannot be NA" when using R with data mining

You can use the removesparseterm function.  Removes sparse ...READ MORE

answered Nov 15, 2018 in Data Analytics by Maverick
• 10,840 points
4,832 views
+1 vote
2 answers
0 votes
1 answer

Trying to find frequent itemsets of a data set using arules package

Try replacing ID <- c("A123","A123","A123","A123","B456","B456","B456") item <- c("bread", "butter", "milk", ...READ MORE

answered Nov 15, 2018 in Data Analytics by Maverick
• 10,840 points
812 views
0 votes
1 answer

Error saying "Error in rnorm() : argument "n" is missing, with no default" in R

Hey @ali, rnorm() function requires an argument, ...READ MORE

answered Oct 30, 2018 in Data Analytics by Maverick
• 10,840 points
3,332 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP