Neo4j v2. Are aggregate functions usable as assignment (set) values? - count

I can return an aggregate such as:
match (u:User)-[:LIKED]->(o:Offer) return count(u) as numLikes
...but I can't assign from it and keep it pre-counted for speed:
match (u:User)-[:LIKED]->(o:Offer) set o.numLikes = count(u)
Is this possible without using two separate statements?

You need to use WITH:
MATCH (u:User)-[:LIKED]->(o:Offer)
WITH o, count(u) AS c
SET o.numLikes = c

You have to complete the aggregation before you can use the aggregated value, you can do this with WITH, something like
MATCH (u:User)-[:LIKED]->(o:Offer)
WITH o, COUNT(u) as numLikes
SET o.numLikes = numLikes

Related

It it possible to use count with a function that matches an expression?

Recently, I had the need of using a simple count function for a analysis workload. The code was something like this:
count(datasetName, variableName %in% c("X330", "X331", "X332", "X333", "X334", "X335", "X336", "X337", "X338", "X339")
Looking at the code, I've been wondering if it is possible to just match the variables names using some sort of matching patterns. I my head, it would look like this:
count(datasetName, variableName %in% match("X33"))
From my research, dplyr contains matching functions, but those expect you to use select. I haven't been able to find how this would work with count.
We can use sum instead of count to get the count of logical vector where TRUE ->1 and FALSE -> 0
with(datasetName, sum(variableName %in% c("X330", "X331", "X332", "X333", "X334", "X335", "X336", "X337", "X338", "X339")))

Delete all duplicated elements in a vector in Julia 1.1

I am trying to write a code which deletes all repeated elements in a Vector. How do I do this?
I already tried using unique and union but they both delete all the repeated items but 1. I want all to be deleted.
For example: let x = [1,2,3,4,1,6,2]. Using union or unique returns [1,2,3,4,6]. What I want as my result is [3,4,6].
There are lots of ways to go about this. One approach that is fairly straightforward and probably reasonably fast is to use countmap from StatsBase:
using StatsBase
function f1(x)
d = countmap(x)
return [ key for (key, val) in d if val == 1 ]
end
or as a one-liner:
[ key for (key, val) in countmap(x) if val == 1 ]
countmap creates a dictionary mapping each unique value from x to the number of times it occurs in x. The solution can then be easily found by extracting every key from the dictionary that maps to val of 1, ie all elements of x that occur precisely once.
It might be faster in some situations to use sort!(x) and then construct an index for the elements of the sorted x that only occur once, but this will be messier to code, and also the output will be in sorted order, which you may not want. The countmap method preserves the original ordering.

XQuery - sum of values of element value in unbounded structure

I am trying to get a sum of values of specific field from the below structure but look like its not working as I am getting error as expected zero or one value but got two or more.
<v4:CalculateResponse xmlns:v4="http://services.xx.net/mm/va">
<v4:CalculateResponseSizeType>
<v4:CalculateCCs>
<v4:Container>
<v4:GrossBookedWeight>31.6</v4:GrossBookedWeight>
<v4:NetPredictedWeight>50</v4:NetPredictedWeight>
<v4:GrossPredictedWeight>53.6</v4:GrossPredictedWeight>
<v4:TypeOfWeightUsed>P</v4:TypeOfWeightUsed>
</v4:Container>
<v4:Container>
<v4:GrossBookedWeight>31.6</v4:GrossBookedWeight>
<v4:NetPredictedWeight>50</v4:NetPredictedWeight>
<v4:GrossPredictedWeight>53.6</v4:GrossPredictedWeight>
<v4:TypeOfWeightUsed>B</v4:TypeOfWeightUsed>
</v4:Container>
<v4:Container>
<v4:GrossBookedWeight>31.6</v4:GrossBookedWeight>
<v4:NetPredictedWeight>50</v4:NetPredictedWeight>
<v4:GrossPredictedWeight>53.6</v4:GrossPredictedWeight>
<v4:TypeOfWeightUsed>B</v4:TypeOfWeightUsed>
</v4:Container>
<v4:Container>
<v4:GrossBookedWeight>31.6</v4:GrossBookedWeight>
<v4:NetPredictedWeight>50</v4:NetPredictedWeight>
<v4:GrossPredictedWeight>53.6</v4:GrossPredictedWeight>
<v4:TypeOfWeightUsed>P</v4:TypeOfWeightUsed>
</v4:Container>
</v4:CalculateCCs>
</v4:CalculateResponseSizeType>
<v4:Status>P</v4:Status>
<v4:StatusCode>1000</v4:StatusCode>
</v4:CalculateResponse>
I have tried summing these values using below function but look like its only onpecting one value.
<Weight>
{
sum(
data($calculateResponse1/*:CalculateResponseSizeType/*:CalculateCCs/*:Container[data(*:TypeOfWeightUsed) = "B"]/*:GrossBookedWeight),
data($calculateResponse1/*:CalculateResponseSizeType/*:CalculateCCs/*:Container[data(*:TypeOfWeightUsed) = "P"]/*:GrossPredictedWeight)
)
}
</Weight>
here calculation is simple, say if TypeOfWeightUsed = 0 then I want to use GrossPredictedWeight element value or if TypeOfWeightUsed = B then I want to use GrossBookedWeight.
we can have multiple container in a structure.
Pls suggest what is wrong with above syntex.
here calculation is simple, say if TypeOfWeightUsed = 0 then I want to use GrossPredictedWeight element value or if TypeOfWeightUsed = B then I want to use GrossBookedWeight.
You can use FLOWR expression with the help of if else construct to get all numbers needed for doing the sum() :
<Weight>
{
sum(
for $c in $calculateResponse1/*:CalculateResponseSizeType/*:CalculateCCs/*:Container
return
if($c/*:TypeOfWeightUsed = "B") then $c/*:GrossBookedWeight
else $c/*:GrossPredictedWeight
)
}
</Weight>
demo
output :
<Weight>170.4</Weight>
When the sum() function has two arguments, the second argument provides a value to be used as the result when the first argument is an empty sequence. (This is a clumsy way of dealing with the fact that without static type checking, the sum() function cannot distinguish an empty sequence of doubles from an empty sequence of durations, and you don't really want an integer-zero result when you are summing durations).
You have called the function with two arguments, but I think you want both sequences to be regarded as inputs to be summed. Just add another pair of parentheses to make it a single argument: replace sum(x, y) by sum((x, y)).
The reason you got an error is that the second argument, if supplied, must be a singleton value, not a sequence.

How to convert a Matrix into a Vector of Vectors?

I have the following matrix A = [1.00 2.00; 3.00 4.00] and I need to convert it into a vector of Vectors as follows:
A1 = [1.00; 3.00]
A2 = [2.00; 4.00]
Any ideas?
tl;dr
This can be very elegantly created with a list comprehension:
A = [A[:,i] for i in 1:size(A,2)]
Explanation:
This essentially converts A from something that would be indexed as A[1,2] to something that would be indexed as A[2][1], which is what you asked.
Here I'm assigning directly back to A, which seems to me what you had in mind. But, only do this if the code is unambiguous! It's generally not a good idea to have same-named variables that represent different things at different points in the code.
NOTE: if this reversal of row / column order in the indexing isn't the order you had in mind, and you'd prefer A[1,2] to be indexed as A[1][2], then perform your list comprehension 'per row' instead, i.e.
A = [A[i,:] for i in 1:size(A,1)]
It would be much better simply to use slices of your matrix i.e. instead of A1 use
A[:,1]
and instead of A2 use
A[:,2]
If you really need them to be "seperate" objections you could try creating a cell array like so:
myfirstcell = cell(size(A,2))
for i in 1:size(A,2)
myfirstcell[i] = A[:,i]
end
See http://docs.julialang.org/en/release-0.4/stdlib/arrays/#Base.cell
(Cell arrays allow several different types of object to be stored in the same array)
Another option is B = [eachcol(A)...]. This returns an variable with type Vector{SubArray} which might be fine depending on what you want to do. To get a Vector{Vector{Float64}} try,
B = Vector{eltype(A)}[eachcol(A)...]

R: build a list from separate key value columns

In R, I'd like to build a key-value paired list from separate key and value columns. In python I would just do something like this:
d = {k:v for k,v in zip(keys, values)}
I want something similar in R that is equivalent to:
list('key1' = 'value1', 'key2' = 'value2', ...)
I've built this with a for-loop but was hoping there is a more elegant R way of doing this.
You can use split to get a list of key/value pair
split(values, keys)

Resources