Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

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

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}
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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