Find values in a table with multiple data types and set them to NA or NaN

1 view (last 30 days)
I have a table (which I've called 'T' in this question) that is approx 105 x 10, with columns 2 & 3 containing strings, and all the rest containing numbers.
In columns 5 through 10 (which only contain numbers), I have some values of 999 interspered in the data that I want to set to NA or NaN.
How can I do this?
I've tried:
idx = T{:,5:10} == 999;
T{idx} = NaN;
T(ismissing(T,{999})) = NaN;
T{T==999}=NaN;
T(T{:,5:10}==999,:) = NaN;
Thank you.

Accepted Answer

Peter Perkins
Peter Perkins on 10 Apr 2019
I think standardizeMissing is the way to go here. It's "straight-forward" to do it explicitly
>> t = table(["a";"b";"c"],[1;999;3],[999;5;999])
t =
3×3 table
Var1 Var2 Var3
____ ____ ____
"a" 1 999
"b" 999 5
"c" 3 999
>> idx = t{:,2:3} == 999
idx =
3×2 logical array
0 1
1 0
0 1
>> t{:,2:3}(idx) = NaN
t =
3×3 table
Var1 Var2 Var3
____ ____ ____
"a" 1 NaN
"b" NaN 5
"c" 3 NaN
but it takes a little thought to get your head around all of what's going on there. Which is why standardizeMissing exists.
>> t = table(["a";"b";"c"],[1;999;3],[999;5;999]);
>> standardizeMissing(t,999)
ans =
3×3 table
Var1 Var2 Var3
____ ____ ____
"a" 1 NaN
"b" NaN 5
"c" 3 NaN

More Answers (0)

Categories

Find more on Tables in Help Center and File Exchange

Tags

Products


Release

R2017a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!