Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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
3  
fn[s_List] := s //. {a_, b_, c_} /; IntegerQ[c] :> {a, b, 1. c} –  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
1  
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 :) –  belisarius Dec 14 '13 at 4:14
2  
Just for the record, N@list or MapAt[N, list, {All, 3}] also works. –  István Zachar Dec 14 '13 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}

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

Your Answer

 
discard

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.