lead

Create lead and lag variables in R

回眸只為那壹抹淺笑 提交于 2019-12-23 06:22:12
问题 I have to create lead and lag variables like below in R Suppose i have a dataframe which has details about a customer's visit to any store... CustomerID Dateofvisit 1 1/2/2013 1 1/3/2013 1 1/7/2013 2 1/9/2013 2 1/14/2013 2 2/14/2013 3 1/4/2013 3 1/5/2013 As we can see, there are 3 customers with different visit dates.. When i apply a lag function on the above...(i created my own function,)..it becomes like below: CustomerID Dateofvisit Laggeddate 1 1/2/2013 - 1 1/3/2013 1/2/2013 1 1/7/2013 1

SQL Server: Lead/Lag analytic function across groups (and not within groups)

会有一股神秘感。 提交于 2019-12-22 04:32:42
问题 Sorry for the long post, but I have provided copy & paste sample data and a possible solution approach below. The relevant part of the question is in the upper part of the post (above the horizontal rule). I have the following table Dt customer_id buy_time money_spent ------------------------------------------------- 2000-01-04 100 11:00:00.00 2 2000-01-05 100 16:00:00.00 1 2000-01-10 100 13:00:00.00 4 2000-01-10 100 14:00:00.00 3 2000-01-04 200 09:00:00.00 10 2000-01-06 200 10:00:00.00 11

R lag/lead irregular time series data

懵懂的女人 提交于 2019-12-19 04:47:13
问题 I have irregular time series data frame with time (seconds) and value columns. I want to add another column, value_2 where values are lead by delay seconds. So value_2 at time t equals to value at time t + delay or right after that. ts=data.frame( time=c(1,2,3,5,8,10,11,15,20,23), value=c(1,2,3,4,5,6,7,8,9,10) ) ts_with_delayed_value <- add_delayed_value(ts, "value", 2, "time") > ts_with_delayed_value time value value_2 1 1 1 3 2 2 2 4 3 3 3 4 4 5 4 5 5 8 5 6 6 10 6 8 7 11 7 8 8 15 8 9 9 20 9

R lag/lead irregular time series data

女生的网名这么多〃 提交于 2019-12-19 04:47:11
问题 I have irregular time series data frame with time (seconds) and value columns. I want to add another column, value_2 where values are lead by delay seconds. So value_2 at time t equals to value at time t + delay or right after that. ts=data.frame( time=c(1,2,3,5,8,10,11,15,20,23), value=c(1,2,3,4,5,6,7,8,9,10) ) ts_with_delayed_value <- add_delayed_value(ts, "value", 2, "time") > ts_with_delayed_value time value value_2 1 1 1 3 2 2 2 4 3 3 3 4 4 5 4 5 5 8 5 6 6 10 6 8 7 11 7 8 8 15 8 9 9 20 9

Lag() with condition in sql server

余生颓废 提交于 2019-12-18 14:54:03
问题 i have a table like this: Number Price Type Date Time ------ ----- ---- ---------- --------- 23456 0,665 SV 2014/02/02 08:00:02 23457 1,3 EC 2014/02/02 07:50:45 23460 0,668 SV 2014/02/02 07:36:34 For each EC I need previous/next SV price. In this case, the query is simple. Select Lag(price, 1, price) over (order by date desc, time desc), Lead(price, 1, price) over (order by date desc, time desc) from ITEMS But, there are some special cases where two or more rows are EC type: Number Price Type

Lead() and LAG() functionality in SQL Server 2008

情到浓时终转凉″ 提交于 2019-12-14 03:56:07
问题 Hope all the SQL GURUS out there are doing great :) I am trying to simulate LEAD() and LAG() functionality in SQL Server 2008. This is my scenario: I have a temp table which is populated using the base query with the business logic for mileage. I want to calculate accumulated mileage for each user per day. The temp table is setup using ROW_NUMBER() , so I have all the data needed in the temp table except the accumulated mileage. I have tried using a CTE with the base query and self joining

SQL: Count of rows between first and last occurrence - with a twist

巧了我就是萌 提交于 2019-12-13 03:50:03
问题 I want to find the count of rows between first and last occurrence of a value. However when there are five or more records of a different value between them, stop counting. So if last occurrence is today and first occurrence is yesterday, the result would be 2 (today plus yesterday). If last occurrence is today and first occurrence is 8 days ago AND there is no occurrence in between the two, the result would be '1'. If however there would be another occurrence 3 days ago, the result would be

LAG/LEAD equivalent with grouping (SQL Server 2008 R2)

早过忘川 提交于 2019-12-12 17:36:22
问题 Note: I am using SQL Server 2008 R2 and built in LEAD/LAG functions are not available. I need to update a table's column to contain the 'previous' and 'next' values for ProductID - the table needs to store PrevProductID (LAG), ProductID and NextProductID (LEAD). The code below does this very nicely and was adapted from Geri Reshef's answer to http://blog.sqlauthority.com/2011/11/24/sql-server-solution-to-puzzle-simulate-lead-and-lag-without-using-sql-server-2012-analytic-function/ USE

SQL: Count of rows since certain value first occurred: keep counting

…衆ロ難τιáo~ 提交于 2019-12-11 11:54:56
问题 This is a similar scenario to SQL: Count of rows since certain value first occurred In SQL Server, I'm trying to calculate the count of days since the same weather as today (let's assume today is 6th August 2018) was observed first in the past 5 days. Per town. Here's the data: +---------+---------+--------+--------+--------+ | Date | Toronto | Cairo | Zagreb | Ankara | +---------+---------+--------+--------+--------+ | 1.08.18 | Rain | Sun | Clouds | Sun | | 2.08.18 | Sun | Sun | Clouds |

What's the opposite function to lag for an R vector/dataframe?

南笙酒味 提交于 2019-12-09 15:55:30
问题 I have a problem dealing with time series in R. #--------------read data wb = loadWorkbook("Countries_Europe_Prices.xlsx") df = readWorksheet(wb, sheet="Sheet2") x <- df$Year y <- df$Index1 y <- lag(y, 1, na.pad = TRUE) cbind(x, y) It gives me the following output: x y [1,] 1974 NA [2,] 1975 50.8 [3,] 1976 51.9 [4,] 1977 54.8 [5,] 1978 58.8 [6,] 1979 64.0 [7,] 1980 68.8 [8,] 1981 73.6 [9,] 1982 74.3 [10,] 1983 74.5 [11,] 1984 72.9 [12,] 1985 72.1 [13,] 1986 72.3 [14,] 1987 71.7 [15,] 1988 72