sum count and max elastic - elasticsearch-5

how can i calculate the number of filter : sum[product.stillDate-Max (product.cutoff)> 0]
example of given:
product: A1- consumer: C1
{
"IdProduct": "A1",
"IdConsumer", "C1",
"StillDate": "2017-10-23T10: 00: 00Z"
"Cutoff": "2017-10-21T00: 00: 00Z"
"LibProduct", "test"
}
product: A1- consumer: C2
{
"IdProduct": "A1",
"IdConsumer": "C2"
"StillDate": "2017-10-23T10: 00: 00Z"
"Cutoff": "2017-10-22T08: 00: 00Z"
"LibProduct": "test1"
}
product: A1- consumer: C3
{
"IdProduct": "A1",
"IdConsumer": "C3"
"StillDate": "2017-10-23T10: 00: 00Z"
"Cutoff": "2017-10-23T08: 00: 00Z"
"LibProduct": "test2"
}
a product A1 is associated with 3 consumers with 3 cutoffs.
the maximum cutoff for product A1 is:
2017-10-23T08: 00: 00Z. So the number is:
sum ((2017-10-23T10: 00: 00Z -2017-10-23T08: 00: 00Z)> 0) => 1
The question how can I do this on kibana?
Thank you for your help and I apologize for my level in English

Related

Inserting words into a table

So, i have a script where i am inputting 3 words in a string and putting them into a small table and i only need this script to print a new table i can copy and paste, so i just need to print what the table would look like this is the script currently have
local tbl = {
{1, "", 0},
{2, "", 0},
{3, "", 0}
}
local input = "i hate debugging"
local words = {}
x = 0
repeat
x = x + 1
for i, v in ipairs(tbl) do
if tonumber(v[1]) == x then
for word in input:gmatch"[^ ,\r\n]+" do
table.insert(words, word)
end
end
end
until x == 1
and the desired output should look like this
{1, "i", 0},
{2, "hate", 0},
{3, "debugging", 0},
The question isn't very clear, and OP goal is not clear, but it seems that you may want something like this:
local tbl = {
{ 1, "", 0 },
{ 2, "", 0 },
{ 3, "", 0 }
}
local input = "i hate debugging"
local nextWord = input:gmatch"[^ ,\r\n]+"
for k, t in ipairs(tbl) do
t[2] = nextWord() or ""
end
-- Display updated `tbl`
print("updated tbl")
for k, v in ipairs(tbl) do
print(string.format('{ %d, "%s", %d },', v[1], v[2], v[3]))
end
gmatch returns an iterator, so you can just call this iterator repeatedly to get successive matches. You can loop over tbl, calling the iterator each time to set the string. When the iterator is out of matches it will return nil, so the code sets the string to "" when the iterator is exhausted.
Program output:
updated tbl
{ 1, "i", 0 },
{ 2, "hate", 0 },
{ 3, "debugging", 0 },
Creating A New Table From Input
OP has clarified in a comment that a new table should be created from the input. Here is a function createWordTable that creates and returns a fresh table from a string of words. The printWordTable function is just a utility function to show the contents of a word table.
function createWordTable(wordString)
local wordTbl = {}
local i = 1
for w in wordString:gmatch"[^ ,\r\n]+" do
wordTbl[i] = { i, w, 0 }
i = i + 1
end
return wordTbl
end
function printWordTable(wordTbl)
for k, v in ipairs(wordTbl) do
print(string.format('{ %d, %s, %d }', v[1], v[2], v[3]))
end
end
Sample interaction:
> wtbl = createWordTable("one, two, three")
> printWordTable(wtbl)
{ 1, one, 0 }
{ 2, two, 0 }
{ 3, three, 0 }
> wtbl = createWordTable("one, two, three, four, five")
> printWordTable(wtbl)
{ 1, one, 0 }
{ 2, two, 0 }
{ 3, three, 0 }
{ 4, four, 0 }
{ 5, five, 0 }

How to merge based on a subset of string in a column?

This is an extension of my previous post.
I have the following dataframes (df1 and df2) that I'm trying to merge:
year <- c("2002", "2002", "1999", "1999", "1997", "2002")
state <- c("TN", "TN", "AL", "AL", "CA", "TN")
name <- c("Molly Homes, Jane Doe", "Sally", "David", "Laura", "John", "Kate")
df1 <- data.frame(year, state, name)
year <- c("2002", "1999")
state <- c("TN", "AL")
versus <- c("Homes (v. Vista)", "#laura v. dAvid")
df2 <- data.frame(year, state, versus)
And I df4 is my ideal output:
year <- c("2002", "2002", "1999", "1999", "1997", "2002")
state <- c("TN", "TN", "AL", "AL", "CA", "TN")
name <- c("Molly Homes, Jane Doe", "Sally", "David", "Laura", "John", "Kate")
versus <- c("Homes (v. Vista)", "# george v. SALLY", "#laura v. dAvid", "#laura v. dAvid", NA, NA)
df4 <- data.frame(year, state, name, versus)
The kind responders on the last post suggested this (and a variation):
library(dplyr)
df3 <- left_join(df1,df2, by=c("year","state")) %>%
rowwise() %>%
mutate(versus:=if_else(grepl(name,versus,ignore.case=T), versus,as.character(NA)))
The problem with the above code is that it doesn't match subsets. Ideally, I'd like grepl(x, y) to match each other, vice versa. If x is in y and/or y is in x, then it's TRUE and results in the value in the "versus" column.
fuzzyjoin is meant for regex searches like this :-)
library(dplyr)
# library(tidyr) # unnest
# library(fuzzyjoin) # fuzzy_*_join
df1 %>%
mutate(
rn = row_number(),
ptn = strsplit(name, "[ ,]+")
) %>%
tidyr::unnest(ptn) %>%
fuzzyjoin::fuzzy_left_join(df2,
by = c("year" = "year", "state" = "state", "ptn" = "versus"),
match_fun = list(`==`, `==`, function(...) Vectorize(grepl)(..., ignore.case = TRUE))
) %>%
group_by(rn, year = year.x, state = state.x, name) %>%
summarize(versus = na.omit(versus)[1], .groups = "drop") %>%
select(-rn)
# # A tibble: 6 x 4
# year state name versus
# <chr> <chr> <chr> <chr>
# 1 2002 TN Molly Homes, Jane Doe Homes (v. Vista)
# 2 2002 TN Sally NA
# 3 1999 AL David #laura v. dAvid
# 4 1999 AL Laura #laura v. dAvid
# 5 1997 CA John NA
# 6 2002 TN Kate NA
We need a way to retrieve the series of whole words, and check if any of them appear (case-insensitive) within the versus column. Here is one simple way to do this:
Create function (f(n,v)), which takes strings n and v, extracts the whole words (wrds) from n, and then counts how many of them are found in v. Returns TRUE if this count exceeds 0
f <- function(n,v) {
wrds = stringr::str_extract_all(n, "\\b\\w*\\b")[[1]]
sum(sapply(wrds[which(nchar(wrds)>1)], grepl,x=v,ignore.case=T))>0
}
Left join the original frames, and apply f() by row, retaining versus if one or more whole words from name are found in veruss, else set to NA
left_join(df1,df2, by=c("year","state")) %>%
rowwise() %>%
mutate(versus:=if_else(f(name, versus), versus,NA_character_))
Output:
1 2002 TN Molly Homes, Jane Doe Homes (v. Vista)
2 2002 TN Sally NA
3 1999 AL David #laura v. dAvid
4 1999 AL Laura #laura v. dAvid
5 1997 CA John NA
6 2002 TN Kate NA
Input:
df1 = structure(list(year = c("2002", "2002", "1999", "1999", "1997",
"2002"), state = c("TN", "TN", "AL", "AL", "CA", "TN"), name = c("Molly Homes, Jane Doe",
"Sally", "David", "Laura", "John", "Kate")), class = "data.frame", row.names = c(NA,
-6L))
df2 = structure(list(year = c("2002", "1999"), state = c("TN", "AL"
), versus = c("Homes (v. Vista)", "#laura v. dAvid")), class = "data.frame", row.names = c(NA,
-2L))

Append three regression tables into separate panels

Consider the following toy data:
input strL Country Population Median_Age Sex_Ratio GDP Trade year
"United States of America" 3999 55 1.01 5000 13.1 2012
"United States of America" 6789 43 1.03 7689 7.6 2013
"United States of America" 9654 39 1.00 7689 4.04 2014
"Afghanistan" 544 24 0.76 457 -0.73 2012
"Afghanistan" 720 19 0.90 465 -0.76 2013
"Afghanistan" 941 17 0.92 498 -0.81 2014
"China" 7546 44 1.01 2000 10.2 2012
"China" 10000 40 0.96 3400 14.3 2013
"China" 12000 38 0.90 5900 16.1 2014
"Canada" 7546 44 1.01 2000 1.2 2012
"Canada" 10000 40 0.96 3400 3.1 2013
"Canada" 12000 38 0.90 5900 8.5 2014
end
I run different regressions (using three different independent variables):
*reg1
local var "GDP Trade"
foreach ii of local var{
qui reg `ii' Population i.year
est table, b p
outreg2 Population using table, drop(i.year*) bdec(3) sdec(3) nocons tex(nopretty) append
}
*reg2
local var "GDP Trade"
foreach ii of local var{
qui reg `ii' Median_Age i.year
est table, b p
outreg2 Population using table2, drop(i.year*) bdec(3) sdec(3) nocons tex(nopretty) append
}
*reg3
local var "GDP Trade"
foreach ii of local var{
qui reg `ii' Sex_Ratio i.year
est table, b p
outreg2 Population using table3, drop(i.year*) bdec(3) sdec(3) nocons tex(nopretty) append
}
I use the append option to append different dependent variables that are to be regressed on the same set of independent variables. Hence, I obtain three different tables.
I wish to "merge" these tables when I compile in LaTeX, so that they appear as a single table, with three different panels, one below the other.
Table1
Table2
Table3
I can use the tex(frag) option of the community-contributed command outreg2, but that will not give me the desired outcome.
Here is a simple way of doing this, using the community-contributed command esttab:
clear
input strL Country Population Median_Age Sex_Ratio GDP Trade year
"United States of America" 3999 55 1.01 5000 13.1 2012
"United States of America" 6789 43 1.03 7689 7.6 2013
"United States of America" 9654 39 1.00 7689 4.04 2014
"Afghanistan" 544 24 0.76 457 -0.73 2012
"Afghanistan" 720 19 0.90 465 -0.76 2013
"Afghanistan" 941 17 0.92 498 -0.81 2014
"China" 7546 44 1.01 2000 10.2 2012
"China" 10000 40 0.96 3400 14.3 2013
"China" 12000 38 0.90 5900 16.1 2014
"Canada" 7546 44 1.01 2000 1.2 2012
"Canada" 10000 40 0.96 3400 3.1 2013
"Canada" 12000 38 0.90 5900 8.5 2014
end
local var "GDP Trade"
foreach ii of local var{
regress `ii' Population i.year
matrix I = e(b)
matrix A = nullmat(A) \ I[1,1]
local namesA `namesA' Population_`ii'
}
matrix rownames A = `namesA'
local var "GDP Trade"
foreach ii of local var{
regress `ii' Median_Age i.year
matrix I = e(b)
matrix B = nullmat(B) \ I[1,1]
local namesB `namesB' Median_Age_`ii'
}
matrix rownames B = `namesB'
local var "GDP Trade"
foreach ii of local var{
regress `ii' Sex_Ratio i.year
matrix I = e(b)
matrix C = nullmat(C) \ I[1,1]
local namesC `namesC' Sex_Ratio_`ii'
}
matrix rownames C = `namesC'
matrix D = A \ B \ C
Results:
esttab matrix(D), refcat(Population_GDP "Panel 1" ///
Median_Age_GDP "Panel 2" ///
Sex_Ratio_GDP "Panel 3", nolabel) ///
gaps noobs nomtitles ///
varwidth(20) ///
title(Table 1. Results)
Table 1. Results
---------------------------------
c1
---------------------------------
Panel 1
Population_GDP .3741343
Population_Trade .0009904
Panel 2
Median_Age_GDP 202.1038
Median_Age_Trade .429315
Panel 3
Sex_Ratio_GDP 18165.85
Sex_Ratio_Trade 27.965
---------------------------------
Using the tex option:
\begin{table}[htbp]\centering
\caption{Table 1. Results}
\begin{tabular}{l*{1}{c}}
\hline\hline
& c1\\
\hline
Panel 1 & \\
[1em]
Population\_GDP & .3741343\\
[1em]
Population\_Trade & .0009904\\
[1em]
Panel 2 & \\
[1em]
Median\_Age\_GDP & 202.1038\\
[1em]
Median\_Age\_Trade & .429315\\
[1em]
Panel 3 & \\
[1em]
Sex\_Ratio\_GDP & 18165.85\\
[1em]
Sex\_Ratio\_Trade & 27.965\\
\hline\hline
\end{tabular}
\end{table}
EDIT:
This preserves the original format:
local var "GDP Trade"
foreach ii of local var{
regress `ii' Population i.year
matrix I = e(b)
matrix A = (nullmat(A) , I[1,1])
local namesA `namesA' `ii'
}
matrix rownames A = Population
matrix colnames A = `namesA'
local var "GDP Trade"
foreach ii of local var{
regress `ii' Median_Age i.year
matrix I = e(b)
matrix B = nullmat(B) , I[1,1]
local namesB `namesB' `ii'
}
matrix rownames B = "Median Age"
matrix colnames B = `namesB'
local var "GDP Trade"
foreach ii of local var{
regress `ii' Sex_Ratio i.year
matrix I = e(b)
matrix C = nullmat(C) , I[1,1]
local namesC `namesC' `ii'
}
matrix rownames C = "Sex Ratio"
matrix colnames C = `namesC'
matrix D = A \ B \ C
Table 1. Results
--------------------------------------
GDP Trade
--------------------------------------
Population .3741343 .0009904
Median Age 202.1038 .429315
Sex Ratio 18165.85 27.965
--------------------------------------

Exporting to text in xojo

I want to export data from a listbox,
Listbox1.AddRow "001", "Orange", "1.00","Arief"
Listbox1.AddRow "001", "Apple", "1.00","Arief"
Listbox1.AddRow "001", "Banana", "1.00","Arief"
Listbox1.AddRow "004", "Orange", "1.00","Arief"
Listbox1.AddRow "005", "Apple", "1.00","Brandon"
Listbox1.AddRow "006", "Banana", "1.00","Brenda"
dim f as folderitem
dim tisx as TextOutputStream
f = new folderitem("item.txt")
tisx = f.CreateTextFile
dim Last_first_word as String
dim maxRow as Integer = Listbox1.listcount-1
for row as integer = 0 to maxRow
if Listbox1.Cell(row,0)<> Last_first_word then
tisx.WriteLine ""
tisx.writeline listBox1.cell(row,0)
tisx.WriteLine listBox1.cell(row,1)+" "+listBox1.cell(row,2)
Last_first_word=Listbox1.Cell(row,0)
else
tisx.WriteLine listBox1.cell(row,1)+" "+listBox1.cell(row,2)
end if
next
tisx.Close
I want to categorized all the items which is has the same code,and put the name at the last.
How to make the result like ,
001
Orange 1.00
Apple 1.00
Banana 1.00
Arief
004
Orange 1.00
Arief
005
Apple 1.00
Brandon
006
Banana 1.00
Brenda
Thanks
Regards,
Arief
You'll need to also save the name so you can display it before you move onto a new group of data. Only a minor tweak to your code was needed:
Listbox1.DeleteAllRows
ListBox1.AddRow("001", "Orange", "1.00", "Arief")
ListBox1.AddRow("001", "Apple", "1.00", "Arief")
ListBox1.AddRow("001", "Banana", "1.00", "Arief")
ListBox1.AddRow("004", "Orange", "1.00", "Arief")
ListBox1.AddRow("005", "Apple", "1.00", "Brandon")
ListBox1.AddRow("006", "Banana", "1.00", "Brenda")
Dim f As FolderItem
Dim tisx As TextOutputStream
f = SpecialFolder.Desktop.Child("item.txt")
tisx = f.CreateTextFile
Dim Last_first_word As String
Dim lastName As String
Dim maxRow As Integer = Listbox1.ListCount - 1
For row As Integer = 0 To maxRow
If Listbox1.Cell(row, 0) <> Last_first_word Then
If lastName <> "" Then tisx.WriteLine(lastName)
tisx.WriteLine("")
tisx.WriteLine(ListBox1.Cell(row, 0))
tisx.WriteLine(ListBox1.Cell(row, 1) + " " + ListBox1.Cell(row, 2))
Last_first_word = ListBox1.Cell(row, 0)
lastName = ListBox1.Cell(row, 3)
Else
tisx.WriteLine(ListBox1.Cell(row, 1) + " " + ListBox1.Cell(row, 2))
End If
Next
If lastName <> "" Then tisx.WriteLine(lastName)
tisx.Close
The data has to be sorted by that group number in order for this to work.

Efficiency of Ruby code: hash of month + frequency into formatted sorted array

I've hacked up some code that fulfills its purpose but it feels very clunky/inefficient. From a table of many entries, each has a month + year string associated with it: "September 2016" etc. From these I create a chronologically ordered array of months and their frequencies to be used in a dropdown selection form: ['Novemeber 2016 (5)', 'September 2016 (5)'].
#months = []
banana = Post.pluck(:month)
#array of all months posted in, eg ['September 2016', 'July 2017', etc
strawberry = banana.each_with_object(Hash.new(0)){|key,hash| hash[key] += 1}
#hash of unique month + frequency
strawberry.each { |k, v| strawberry[k] = "(#{v.to_s})" }
#value into string with brackets
pineapple = (strawberry.sort_by { |k,_| Date.strptime(k,"%b %Y") }).reverse
#sorts into array of months ordered by most recent
pineapple.each { |month, frequency| #months.push("#{month}" + " " + "#{frequency}") }
#array of formatted months + frequency, eg ['July 2017 (5)', 'September 2016 (5)']
I was hoping some of the Ruby gurus here could advise me in some ways to improve this code. Any ideas or suggestions would be greatly appreciated!
Thanks!
['September 2016', 'July 2017', 'September 2016', 'July 2017']
.group_by { |e| e } # .group_by(&:itself) since Ruby2.3
.sort_by { |k, _| Date.parse(k) }
.reverse
.map { |k, v| "#{k} (#{v.count})" }
#⇒ [
# [0] "July 2017 (2)",
# [1] "September 2016 (2)"
# ]

Resources