Group Abstract Group Abstract

Message Boards Message Boards

0
|
1.9K Views
|
5 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Question about applying a function to dataset values

Posted 11 months ago

The attached notebook explains a problem that I'm grappling with in which I want to programatically apply an evaluated expression, including a function and named variables, to all values in a dataset. Any advice on how to achieve the desired result shown at the end of the notebook would be much appreciated. Thanks in anticipation...

POSTED BY: Ian Williams
5 Replies
POSTED BY: Ian Williams
Posted 11 months ago
POSTED BY: Eric Rimbey

Thanks Eric. I've made some modifications to your code and got it working - see final section of the attached notebook. I would, however, still like to understand why using the variable 'expression' in my original notebook failed whereas pasting the evaluated form of 'expression' gave the desired result. I suspect it'll be to do with the evaluation process. But since using variables for keys in Slot is a useful thing to be able to do so I'd like to understand how it works (or, in this case, doesn't). Thanks again, Ian

POSTED BY: Ian Williams
Posted 11 months ago

Fixed

POSTED BY: Eric Rimbey
Posted 11 months ago

I would go about it like this (there are probably refinements, but this is a working starting point):

  • Create a function to map keys to types:

    MapThread[(keyType[#1] = #2) &, {keys, types}]
    
  • An alternate to your toType function (I simplified it a bit, but if any of this is wrong you can figure out how to adjust it):

    typedVal[type_String, ""] := Missing["Empty string", type];
    typedVal[type_String?(StringEndsQ[{"DP", "SF"}]), val_String] := ToExpression[val];
    typedVal[type_String?(StringEndsQ[{"SCI", "U"}]), val_String] := Interpreter["Number"][val];
    typedVal[type_String, val_] := val
    

    You don't actually need to name the first argument, since we don't actually need to reference that argument's name (except for the first form, which is probably overkill anyway).

  • Create a function to map the original pairs to pairs with typed values:

    toTypedPair[key_String, val_] := key -> typedVal[keyType[key], val]
    
  • Apply it to ds:

    ds[All, Association@*KeyValueMap[toTypedPair]]
    
POSTED BY: Eric Rimbey
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard