2

What is the most elegant way to replace a value(say 99) to NA in a DataFrame in Julia?

I am considering using an iterator to skim each row and check each value for 99, etc. I would like to know if there is a better way to do this.

using Taro
Taro.init()
tempdf =Taro.readxl("/path/to/xls/", "Sheet1", "A1:CW24983"; header=false)
#Need one line command to replace all the 99s in the 24983 X 101 DataFrame to NA

Here is the MWE :

Pkg.add("RDatasets")
using RDatasets, DataFrames
datafr = dataset("datasets","anscombe")
#Replace each instance of 10(for e.g : (1,X1), (1,X2)) with NA

Edit 1 : Question for Julia similar to one here for R.

Community
  • 1
  • 1
envy_intelligence
  • 453
  • 1
  • 6
  • 21
  • 7
    You can go over each column this way: `[d[d[nm] .== 99, nm] = NA for nm in names(d)]`, though there may be a more elegant solution. – jverzani Jun 16 '15 at 12:25

2 Answers2

1

Thanks jverzani

[d[d[nm] .== 99, nm] = NA for nm in names(d)]

Community
  • 1
  • 1
envy_intelligence
  • 453
  • 1
  • 6
  • 21
  • 7
    It's good that your problem is solved, but taking a suggestion from a comment to your question and posting it as a self answer is not very fair. Why not invite jverzani to post an answer instead? – jub0bs Jun 16 '15 at 13:49
  • The other way: filling all NA of a DataFrame to 0: `[t[isna(t[nm]), nm] = 0 for nm in names(t)]` – Antonello Mar 17 '17 at 12:44
  • NA apparently no longer has any meaning in Julia 1.x, and trying to assign to NaN generates an error. – johnjps111 Feb 24 '19 at 23:37
0

How about this: tempdf = DataFrame(replace!(convert(Matrix, tempdf), 99=>"NA")) ?

srgk26
  • 21
  • 2