I have a long list of triples, each looking something like {AGO, 1988, 2345.23}. Some of these, however, have an integer in the third spot, like this: {IND, 1993, 4345} because of the nature of the original data. I do not want integers, because I have to Log this data eventually.

So I thought I would map the following function through my list:

fn[s_List] :=  s //. {a_, b_, c_} /; IntegerQ[c] -> {a, b, N[c]}

It does not work. I want it to put "4345." in place of "4345".

However, I can use the following to get a very good approximation:

fnn[s_List] :=  s //. {a_, b_, c_} /; IntegerQ[c] -> {a, b, c-.00001}

But, even though the loss of precision is not important for this project, I'd like to know why my first function does not work.

I have been working with these kinds of replacement rules a lot recently, and this one seems pretty simple.

Any help is very much appreciated.

  • 3
    $\begingroup$ fn[s_List] := s //. {a_, b_, c_} /; IntegerQ[c] :> {a, b, 1. c} $\endgroup$ Dec 14, 2013 at 4:03
  • $\begingroup$ In addition to what @belisarius said, the ReplaceRepeated (//.) should be unnecessary. Try ReplaceAll (/.). $\endgroup$
    – Michael E2
    Dec 14, 2013 at 4:06
  • 1
    $\begingroup$ Here's a clean way: fn[s_List] := s /. {a_, b_, c_Integer} :> {a, b, N@c} $\endgroup$
    – RunnyKine
    Dec 14, 2013 at 4:09
  • $\begingroup$ @MichaelE2 Yep. My blinkers only allowed me to see the IntegerQ thing :) $\endgroup$ Dec 14, 2013 at 4:14
  • 2
    $\begingroup$ Just for the record, N@list or MapAt[N, list, {All, 3}] also works. $\endgroup$ Dec 14, 2013 at 12:46

1 Answer 1


Just so this question has an answer, the following works:

lis = {{ago, 1988, 2344}, {bgy, 1980, 6654.5}, {ccr, 1999, 646}};

Now define:

fn[s_List] := s /. {a_, b_, c_} /; IntegerQ[c] :> {a, b, N@c}




{{ago, 1988, 2344.}, {bgy, 1980, 6654.5}, {ccr, 1999, 646.}}

Here is a shorter, cleaner way to achieve the same thing:

fn[s_List] := s /. {a_, b_, c_Integer} :> {a, b, N@c}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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