Access a column programmatically on a table expression
See original GitHub issueApologies if this shouldn’t be an issue, but I have been searching everywhere and haven’t been able to understand what can I do with table expressions. Is this documented?
I want to derive columns programmatically. I have a dataset with a bunch of dates per each row, and I want to compute their relative position compared to a reference one. I know I can do:
dt.derive({
date1_rel: d => d.date1 - d.date_ref,
date2_rel: d => d.date2 - d.date_ref,
...
})
But I don’t want to write that by hand. I tried then these cells on Observable:
columnFechas = [
"fis",
"fecha_de_muerte",
"fecha_reporte_web"
"fecha_recuperado",
"fecha_de_notificaci_n",
"fecha_diagnostico"
]
deriveFechas = Object.fromEntries(
columnFechas.map(f => [
`${f}_res`,
d => d[f] - d.fecha_reporte_web
])
)
dt.derive(deriveFechas)
But It throws an error saying Invalid column "f": "d[f]"
. I tried debugging the code and noticed that the function is a table expression, so for a reason I don’t understand I cannot even console.log the attribute d. How do I get the column that is in the variable f
in a table expression. Documenting this could be important whenever you have column names with spaces
Here is a notebook replicating the issue
https://observablehq.com/d/2783d075d8079564#fechas
Seems related to #5, is there a way of doing op.get(column, d)
?
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (6 by maintainers)
Top GitHub Comments
A question I continue to ask myself despite years of working with Jeff (perhaps with even more question marks now that I’m a faculty member as well 😃
BTW the new documentation for expression looks amazing! How can you do this so quickly?!