2
$\begingroup$

I have this list:

a = {{{0, 0}, {1, 7}, {2, 0}, {3, 2}, {4, 7}}, {{0, 0}, {1, 0}, {2, 1}, {3, 2}, {4, 7}}}

and I'd like to transform it to this:

a = {{{0, 0}, {1, 7}, {2, Na}, {3, 2}, {4, 7}}, {{0, 0}, {1, Na}, {2, 1}, {3, 2}, {4, 7}}}

ie I'd like to replace every 0 by Na except in the first sub-sub-list of each sub-list.

All I can come up with is:

Part[#, 2 ;; All, All] & /@ a  /. 0 -> Na

and it works I get

{{{1, 7}, {2, Na}, {3, 2}, {4, 7}}, {{1, Na}, {2, 1}, {3, 2}, {4, 7}}}

but the modification is not attributed to a.

How can I do that?

$\endgroup$
2
  • $\begingroup$ a = Part[#, 2 ;; All, All] & /@ a /. 0 -> Na or perhaps you intended to modify the original copy of a ? $\endgroup$ Jan 8 '13 at 12:10
  • $\begingroup$ yes I need to modify the original copy of a your solution below works thanks! $\endgroup$
    – Sulli
    Jan 8 '13 at 12:58
3
$\begingroup$

This will work if all your first elements of the sub-lists are of the form {0,0}

a = a /. {{x_?Positive, 0} :> {x, Na}};
a

{{{0, 0}, {1, 7}, {2, Na}, {3, 2}, {4, 7}}, {{0, 0}, {1, Na}, {2, 1}, {3, 2}, {4, 7}}}

$\endgroup$
2
$\begingroup$

Another quick possibility

a=a/. {n_, r:{_Integer,_Integer}...} :> Join[{n},{ r} /. 0->Na]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.