-2

I am doing calculation on columns using summation. I want to manually change my first n entries in my calc column from float to NaN. Can someone please advise me how to do that?

For example, if my column in table t now is mycol:(1 2 3 4 5 6 7 8 9), I am trying to get a function that can replace the first n=4 entries with NaN, so my column in table t becomes mycol:(0N 0N 0N 0N 5 6 7 8 9)

Thank you so much!

Emily

Will Da Silva
  • 6,386
  • 2
  • 27
  • 52

1 Answers1

1

We can use amend functionality to replace the first n items with null value. Additionally, it would be better to use the appropriate null literal for each column based on the type. Something like this would work:

f: {nullDict: "ijfs"!(0Ni;0Nj;0Nf:`); @[x; til y; :; nullDict .Q.ty x]}

This will amend the first y items in the list x. .Q.ty will get the type for input so that we can get the corresponding value from the dictionary.

You can then use this for a single column, like so:

update mycol: f[mycol;4] from tbl

You can also do this in one go for multiple columns with varying number of items to be replaced using functional form:

![tbl;();0b;`mycol`mycol2!((f[;4];`mycol);(f[;3];`mycol2))]

Do take note that you will need to modify nullDict with whatever other types you need.

Update: Thanks to Jonathon McMurray for suggesting a better way to build up nullDict for all primitive types using the below code:

{x!first each x$\:()}.Q.t except " "
shree.pat18
  • 21,449
  • 3
  • 43
  • 63