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 list in the form {{a, 1}, {b, 2}, {c, 3}}.

Let's say I want to operate a transformation on all the second elements of each sublist, but this transformation is a function of them all together. As an example I want to Standardize them.

At the moment I've implemented this with Transpose.

mylist = {{a, 1}, {b, 2}, {c, 3}};
newlist = Transpose[{mylist[[All, 1]], Standardize[mylist[[All, 2]]]}]

and the output is correctly

{{a, -1}, {b, 0}, {c, 1}}

I find this a little cumbersome, this approach can become complicated and hard to read sometimes. Isn't there a more natural way?

share|improve this question
2  
You could do newlist = mylist; newlist[[All, 2]] = Standardize[newlist[[All, 2]]];, but I don't consider it any more natural than what you have. –  J. M. Jun 10 '13 at 15:30
    
I am positive this question is a duplicate, though perhaps the original is on StackOverflow. I'd appreciate help finding it. –  Mr.Wizard Jun 10 '13 at 15:32
    
This is related but it does not expressly describe passing the entire column to the function; I know there is a question that does. –  Mr.Wizard Jun 10 '13 at 15:37
1  
Another slight variation (but hardly more natural either): MapAt[Standardize, #\[Transpose], 2]\[Transpose] &@mylist –  TomD Jun 10 '13 at 16:54
    
Duh, deleting my remark, which is just a repeat of what 0x4A4D said. But I like that method. –  Todd Gayley Jun 10 '13 at 18:18

2 Answers 2

I'd suggest...

Apply[Transpose[{#1,Standardize[#2]}]&,Transpose[mylist]]
share|improve this answer

Here's a small variation on the solutions proposed in the comments:

Thread[{First[Thread[myList]], Standardize[Last[Thread[myList]]]}]
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.