matrix - how to apply matrix computations only to specific columns
Closed this issue · 4 comments
hi,
I have arrays which have the following format:
note: I simplified this a bit...
array1 = [
{date: "2018-07-01T00:00:00", value: null},
{date: "2018-07-02T00:00:00", value: 1.00},
{date: "2018-07-03T00:00:00", value: 2.00},
...
]
array2 = [
{date: "2018-07-01T00:00:00", value: 11.00},
{date: "2018-07-02T00:00:00", value: 22.00},
{date: "2018-07-03T00:00:00", value: 33.00},
...
]
On this arrays I would like to perform some matrix computations like
let df1 = new DataFrame(array1);
df_result = df1.matrix.add(df2);
because I do not want to concatenate the date strings, I would like to add only the values of key "value".
I'm sure this can be done :) but how?
Hi @udos,
Indeed it's a bit unusual, the matrix add
method applying on all columns and all rows.
If you have the same dates in the same order you can do:
const dates = df1.toArray("date");
const result = df1.matrix.add(df2).withColumn("date", (_, index) => dates[index]);
or in the next release (1.4.0 which is out in few days):
const result = df1.matrix.add(df2).withColumn("date", (_, index) => df1.getRow(index).get("date"));
It's a bit tricky thus I will consider an easier way for the next releases.
Tell me if it works.
PS: Feel free to test the next release by pulling the develop branch.
hi @Gmousse,
as always quick and to the point. both versions work like a charm!
thank you very much,
udo
p.s.: and thanks for the "difficulty:easy" tag :| -> I will focus on more difficult questions :)
related question:
in array1, the value = null
. currently, it is treated as 0. in my case this is not ok. is there an option to set so if a value is null
, the result of a calculation would be null
as well?
Hi @udos, you r welcome !
I will focus on more difficult questions :)
Ahah, I just set the difficulty to easy when it's not related (directly) to a code modification.
About your question:
It's not due to my code but it's related to the javascript +
operator.
Examples:
null + 1
// 1
NaN + 1
// NaN
To resume, you can replace your null values by NaN if you want this behaviour.
I hope this answer helps.
helps a lot! you always help! thanks!
p.s.: big relief regarding
I just set the difficulty to easy when it's not related (directly) to a code modification.
:)