# Convert Integer to Numeric with Replacement rules

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.

• fn[s_List] := s //. {a_, b_, c_} /; IntegerQ[c] :> {a, b, 1. c} Dec 14 '13 at 4:03
• In addition to what @belisarius said, the ReplaceRepeated (//.) should be unnecessary. Try ReplaceAll (/.). Dec 14 '13 at 4:06
• Here's a clean way: fn[s_List] := s /. {a_, b_, c_Integer} :> {a, b, N@c} Dec 14 '13 at 4:09
• @MichaelE2 Yep. My blinkers only allowed me to see the IntegerQ thing :) Dec 14 '13 at 4:14
• Just for the record, N@list or MapAt[N, list, {All, 3}] also works. 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}


Then:

fn[lis]


Gives:

{{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}