question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Access a column programmatically on a table expression

See original GitHub issue

Apologies 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:closed
  • Created 3 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
arvindcommented, Oct 5, 2020

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 😃

2reactions
john-guerracommented, Oct 5, 2020

BTW the new documentation for expression looks amazing! How can you do this so quickly?!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using by in data.table programmatically with an operation on ...
I want to use a character vector containing column names in by in data.table along with the interactive way of defining groups. The...
Read more >
Create a new column programmatically in Access - Office
Describes how to programmatically create a column in an Access report. Provides two methods to dynamically add the column to the report.
Read more >
improvement on programmatically substituting columns in ...
I agree with Jan re preferring expressions over strings. One other idea: add a function like get / mget , but that only...
Read more >
Adding Table Columns in Perspective with Script or Expression
I am trying to change the number of columns based on what table is… ... Is it possible to add columns to table...
Read more >
Introduction to data.table
This vignette introduces the data.table syntax, its general form, how to subset rows, select and compute on columns, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found