我有一个这样的表:
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
Run Code Online (Sandbox Code Playgroud)
对于每个EC,我需要上一个/下一个SV价格.在这种情况下,查询很简单.
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
Run Code Online (Sandbox Code Playgroud)
但是,有一些特殊情况,其中两行或更多行是EC类型:
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
23658 2,4 EC 2014/02/02 07:50:45
23660 2,4 EC 2014/02/02 …Run Code Online (Sandbox Code Playgroud) 我有一个小标题,每行有一个单词列表。我想从一个搜索关键字的函数中创建一个新变量,如果找到该关键字,则创建一个由关键字正负3个单词组成的字符串。
下面的代码是close,但是不是抓住我的关键字之前和之后的所有三个单词,而是抓住单词3前后的单词。
df <- tibble(words = c("it", "was", "the", "best", "of", "times",
"it", "was", "the", "worst", "of", "times"))
df <- df %>% mutate(chunks = ifelse(words=="times",
paste(lag(words, 3),
words,
lead(words, 3), sep = " "),
NA))
Run Code Online (Sandbox Code Playgroud)
最直观的解决方案是该lag函数可以执行以下操作:lead(words, 1:3)但这不起作用。
显然,我可以手动(paste(lead(words,3), lead(words,2), lead(words,1),...lag(words,3))很快完成此操作,但实际上,我最终将希望能够抓住50个单词左右的关键字,以至于无法手工编码。
如果tidyverse中存在解决方案,那将是理想的选择,但是任何解决方案都将有所帮助。任何帮助,将不胜感激。
很抱歉这篇文章很长,但我提供了复制和粘贴示例数据以及下面的可能解决方案.的的相关部分的问题是在后(水平线以上)的上部.
我有下表
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
2000-01-06 200 11:00:00.00 5
2000-01-10 200 08:00:00.00 20
Run Code Online (Sandbox Code Playgroud)
并希望查询获取此结果集
Dt Dt_next customer_id buy_time money_spent
-------------------------------------------------------------
2000-01-04 2000-01-05 100 11:00:00.00 2
2000-01-05 2000-01-10 100 16:00:00.00 1
2000-01-10 NULL 100 13:00:00.00 4
2000-01-10 NULL 100 14:00:00.00 3
2000-01-04 2000-01-06 200 09:00:00.00 10
2000-01-06 2000-01-10 200 10:00:00.00 11
2000-01-06 2000-01-10 …Run Code Online (Sandbox Code Playgroud) 我在处理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)
Run Code Online (Sandbox Code Playgroud)
它给了我以下输出:
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.9
[16,] 1989 75.3
[17,] 1990 81.2
[18,] …Run Code Online (Sandbox Code Playgroud) 我需要查询一个包含步骤id +值的表.结果将列出间隔及其相关值.这里的间隔被定义为"连续的步骤ID,共享相同的数据值".
我很难用文字描述它,所以请看这个:
从这张桌子
Step ! Data
------------
1 ! A
2 ! A
3 ! A
5 ! A
6 ! B
10 ! A
Run Code Online (Sandbox Code Playgroud)
我需要以下报告
From ! To ! Data
-------------------
1 ! 3 ! A
5 ! 5 ! A
6 ! null ! B
10 ! null ! A
Run Code Online (Sandbox Code Playgroud)
我认为lead()会帮助我,但没有成功.
我一直在工作,看不到Oracle前导和滞后功能的等效功能.
oracle领导看起来像
LEAD(col1.date,1,ADD_MONTHS(col1.DATE,12))
OVER(Partition By tab.a,tab.b,tab.c Order By tab.a)-1 END_DATE
LAG(col1.DATE + 7,1,col1.DATE-1)
OVER(partition by tab.a,tab.b Order By tab.b) LAG_DATE
Run Code Online (Sandbox Code Playgroud)
任何更好的主意
我有一个这样的表:
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
23461 0,668 SV 2014/02/02 07:37:34
23462 0,668 SV 2014/02/02 07:38:34
23463 0,668 SV 2014/02/02 07:39:34
Run Code Online (Sandbox Code Playgroud)
对于每条记录,我需要上一个/下一个价格.在这种情况下,查询很简单.
Select Lag(price) over (order by date desc, time desc),
Lead(price) over (order by date desc, time desc)
from ITEMS
Run Code Online (Sandbox Code Playgroud)
但我需要结果Where Next price <>记录价格
我的查询是
Select Lag(price) over (order by date desc, time desc) Nxt_Price,
Lead(price) over (order by …Run Code Online (Sandbox Code Playgroud) 我需要一些指导并帮助解决一个问题我不完全确定如何在SQL Server 2012中解决.我认为LAG和LEAD功能可能有用,但我不确定.
这就是我现在的数据:
=========================================
YearMonth LocationCode Active
=========================================
201405 123 0
201406 123 2
201409 211 1
201410 211 0
201411 214 0
201412 214 3
Run Code Online (Sandbox Code Playgroud)
我们有一个YearMonth列显示每个状态的状态,locationCode以及Active表示每个状态的int 的列LocationCode
目的:
我的目标是比较LocationCode当前YearMonth(让我们称之为201406)和前一个Yearmonth(让我们称之为201405)的for :
一个例子 :
=========================================
YearMonth LocationCode Active
=========================================
201405 123 0
201406 123 2
Run Code Online (Sandbox Code Playgroud)
基本上我想弄清楚的是如何将当前月份的行(201406)与上一个月的行(201405)进行比较Active.
如果当前月份的行列Active为非零且前一个月的活动为零,则我们将当前月份的行结束为"新"(1)否则为(0).
下面提供了一个示例:
==================================================
YearMonth LocationCode Active New
===================================================
201405 …Run Code Online (Sandbox Code Playgroud) Facebook页面Leadgen Webhook不适用于我的页面和相关应用程序。
如果我使用测试工具测试我设置的Webhook ,然后单击“跟踪状态”按钮,则发出的实时更新对象与此错误相关:102 Server failure
我的Facebook设置包括:
页面,ac accoung和应用程序都属于公司,并且该应用程序似乎已正确订阅该页面:它在对的图形api调用的结果中列出/<page-id>/subscribed-apps。
有谁知道我可能会想念什么?
非常感谢。
不明白为什么我的超前和滞后功能忽略了group by。这是一个简单的例子(实际上我需要按 5 列分组)。?
# Dummy DataSet
df <- data.frame(group = c("a","a","a","a", "a", "b", "b", "b", "b", "b"),
order = c(3, 4, 2, 5, 1, 1, 3, 4, 2, 4),
value = c(15, 22, 43, 31, 25, 11, 37, 24, 18, 9))
"group" "order" "value"
"a" 3 15
"a" 4 22
"a" 2 43
"a" 5 31
"a" 1 25
"b" 1 11
"b" 3 37
"b" 4 24
"b" 2 18
"b" 4 9
Run Code Online (Sandbox Code Playgroud)
试过这个,但即使是 order by 在这里也不起作用
df …Run Code Online (Sandbox Code Playgroud)