Grokbase Groups R r-help July 2012
FAQ
Hi,
Let's say I have the following data:
a=matrix(c(1,2,4,4,2,1,1,2,4),nrow=3,byrow=T)
a
[,1] [,2] [,3]

[1,] 1 2 4

[2,] 4 2 1

[3,] 1 2 4

What syntax should I use to get R to tell me the column that corresponds to
the maximum value for each row?

For my example, I would like to get a vector that says 3, 1, 3 because the
maximum value for row 1 is in column 3, the maximum value for row 2 is in
column 1, and the maximum value for row 3 is in column 3.

Does that make sense?

Please cc me if you reply!

Chris

Search Discussions

  • R. Michael Weylandt at Jul 17, 2012 at 10:08 pm
    apply(a, 1, which.max)

    Apply the which.max() function to the matrix a row-wise (1)

    Michael

    On Tue, Jul 17, 2012 at 5:03 PM, Christopher Desjardins
    wrote:
    Hi,
    Let's say I have the following data:
    a=matrix(c(1,2,4,4,2,1,1,2,4),nrow=3,byrow=T)
    a
    [,1] [,2] [,3]

    [1,] 1 2 4

    [2,] 4 2 1

    [3,] 1 2 4

    What syntax should I use to get R to tell me the column that corresponds to
    the maximum value for each row?

    For my example, I would like to get a vector that says 3, 1, 3 because the
    maximum value for row 1 is in column 3, the maximum value for row 2 is in
    column 1, and the maximum value for row 3 is in column 3.

    Does that make sense?

    Please cc me if you reply!

    Chris

    [[alternative HTML version deleted]]

    ______________________________________________
    R-help at r-project.org mailing list
    https://stat.ethz.ch/mailman/listinfo/r-help
    PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
    and provide commented, minimal, self-contained, reproducible code.
  • Arun at Jul 17, 2012 at 10:17 pm
    Hi,
    Try,
    apply(a,1,which.max)
    [1] 3 1 3

    A.K.



    ----- Original Message -----
    From: Christopher Desjardins <cddesjardins@gmail.com>
    To: r-help at r-project.org
    Cc:
    Sent: Tuesday, July 17, 2012 6:03 PM
    Subject: [R] Finding the column with the maximum value by row

    Hi,
    Let's say I have the following data:
    a=matrix(c(1,2,4,4,2,1,1,2,4),nrow=3,byrow=T)
    a
    ? ? [,1] [,2] [,3]

    [1,]? ? 1? ? 2? ? 4

    [2,]? ? 4? ? 2? ? 1

    [3,]? ? 1? ? 2? ? 4

    What syntax should I use to get R to tell me the column that corresponds to
    the maximum value for each row?

    For my example, I would like to get a vector that says 3, 1, 3 because the
    maximum value for row 1 is in column 3, the maximum value for row 2 is in
    column 1, and the maximum value for row 3 is in column 3.

    Does that make sense?

    Please cc me if you reply!

    Chris

    ??? [[alternative HTML version deleted]]

    ______________________________________________
    R-help at r-project.org mailing list
    https://stat.ethz.ch/mailman/listinfo/r-help
    PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
    and provide commented, minimal, self-contained, reproducible code.
  • Guillaume Bal at Jul 17, 2012 at 10:40 pm
    Hi,

    You should be able to reach your goal using functions "which" and "apply"
    On 7/17/2012 3:03 PM, Christopher Desjardins wrote:
    Hi,
    Let's say I have the following data:
    a=matrix(c(1,2,4,4,2,1,1,2,4),nrow=3,byrow=T)
    a
    [,1] [,2] [,3]

    [1,] 1 2 4

    [2,] 4 2 1

    [3,] 1 2 4

    What syntax should I use to get R to tell me the column that corresponds to
    the maximum value for each row?

    For my example, I would like to get a vector that says 3, 1, 3 because the
    maximum value for row 1 is in column 3, the maximum value for row 2 is in
    column 1, and the maximum value for row 3 is in column 3.

    Does that make sense?

    Please cc me if you reply!

    Chris

    [[alternative HTML version deleted]]

    ______________________________________________
    R-help at r-project.org mailing list
    https://stat.ethz.ch/mailman/listinfo/r-help
    PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
    and provide commented, minimal, self-contained, reproducible code.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupr-help @
categoriesr
postedJul 17, '12 at 10:03p
activeJul 17, '12 at 10:40p
posts4
users4
websiter-project.org
irc#r

People

Translate

site design / logo © 2017 Grokbase