-
Hello, thanks for the great package! I'm still learning it and I got stuck at the the following task, which should be easy... I have a table like this: const t = aq.table({
group: ["a", "a", "a", "b", "b", "b", "b", "b"],
answer: ["y", "n", "y", "y", "n", "n", "n", "y"]
})
I"d like to convert it to this:
I know, it is easy to use t.groupby("group").rollup({
y_count: (d) => d.filter((x) => x.answer === "y").count(),
n_count: (d) => d.filter((x) => x.answer === "n").count()
}) But seems that filtering inside rollup not working as it returns same counts for both column (total rows). Also I tried to use Another idea was to chain groupby with pivot: t.groupby("group").pivot("answer", { value: x => x.count() }) but I also got the same result - total number of rows in group without splitting by answer. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
Hmm, it looks like In the meantime, here is an alternative that works using sum: t.groupby('group')
.pivot('answer', { value: () => op.sum(1) }) |
Beta Was this translation helpful? Give feedback.
-
Except this, it works like a charm! I used it to calculate Kaplan-Meier survival function. Before arquero it was a mix of d3/lodash/loops/nested function calls and now it is just a chain of straightforward operations. Especially I liked, how at first I implemented everything for all data, and then just inserted single const KMtable = aq
.from(data)
.params({ split })
.derive({
age_days: (d) => +d.age_days,
group: (d, $) => $.split ? aq.substring(d.created_at, 0, 4) : "all"
})
.groupby("group", "age_days")
.rollup({
closed: (d) => aq.sum(d.is_closed),
open: (d) => aq.sum(!d.is_closed)
})
.groupby("group")
.orderby(aq.desc("age_days"))
.derive({ cum_open: aq.rolling((d) => aq.sum(d.open + d.closed)) })
.derive({ hazard: (d) => d.closed / d.cum_open })
.orderby("age_days")
.derive({ survival: aq.rolling((d) => aq.product(1 - d.hazard)) })
.derive({ survival: (d) => aq.lag(d.survival) || 1 })
.objects(); |
Beta Was this translation helpful? Give feedback.
Hmm, it looks like
pivot
is not handlingcount()
as expected. I'll need to look into that.In the meantime, here is an alternative that works using sum: