[R] how to apply several math/logic operations on columns/rows of a matrix
Marc Jekel
jekel at coll.mpg.de
Fri Jul 1 19:31:19 CEST 2011
Dear R-Fans,
The more I work with matrices (e.g., data.frames) the more I think it
would be helpful to have functions to apply (several!) mathematical
and/or logical operators column- or row-wise in a matrix.
I know the function apply() and all its derivates (e.g., lapply) but I
think this does not help for solving (e.g.) the following task:
assume there is a 3x3 matrix:
1 2 4
4 5 3
1 3 4
How do I find - for each column separately - the position of the
column's minimum without using loop commands, i.e.:
I could extract each column in a loop and use something like:
for (loopColumn in 1 : 3){
extractedColumnVector = myMatrix[, loopColumn]
position = which(extractedColumnVector == min (extractedColumnVector ) )
print(position)
}
I think that there should be something simpler out there to handle these
kinds of tasks (maybe there is and I just don't know but I checked
several R books and could not find a command to do this).
It would be great to have a function in which it is possible to define a
sequence of commands that can be applied column/row-wise.
Thanks for a hint,
Marc
--
Dipl.-Psych. Marc Jekel
MPI for Research on Collective Goods
Kurt-Schumacher-Str. 10
D-53113 Bonn
Germany
email: jekel at coll.mpg.de
phone: ++49 (0) 228 91416-852
http://www.coll.mpg.de/team/page/marc_jekel-0
More information about the R-help
mailing list