从R的数据框中的不同列获取最大值

时间:2017-08-02 03:46:09

标签: r data-science

我有一个数据框如下:

<div class="form-group">
      <a class="btn btn-default btn-select">
            <input class="btn-select-input" id="" name="" value="Option 2" type="hidden">
            <span class="btn-select-value">Purpose of Enquiry</span>
            <span class="btn-select-arrow glyphicon glyphicon-triangle-bottom"></span>
            <ul style="display: none;">
                <li>Career Enquiries</li>
                <li>Data Protection Feedback</li>
                <li>Media & Publications</li>
                <li>New Business Collaborations<input class="dropinput" type="text" placeholder="Please specify project location"></li>
                <li>Others<input class="dropinput" type="text" placeholder="Please specify"></li>
            </ul>
        </a>
        </div>

请告诉我R命令给出A的最大值,B的最大值,C的最大值和D的最大值的相应H值,并将数据存储在矢量中。

col A中的最大值为5,col B中的最大值为67,col C中的最大值为9,D中的最大值为17.

我的新矢量应该有H引用为'W','W','Z','Z'

由于

3 个答案:

答案 0 :(得分:3)

data$H[sapply(data[,-1],which.max)]

答案 1 :(得分:2)

以下是使用base R

的选项
m1 <- t(df1[-1])
df1[,1][max.col(replace(m1, is.na(m1), 0), "first")]
#[1] "W" "W" "Z" "Z"

答案 2 :(得分:1)

尝试使用class Object def puts(*args) super yield if block_given? end end puts("mindful") { "of blocks" } # "mindful" # => "of blocks" reshape

dplyr

如果您需要DF=melt(df) DF=DF%>%group_by(variable)%>%slice(which.max(value)) DF # A tibble: 4 x 3 # Groups: variable [4] H variable value <chr> <fctr> <int> 1 W A 5 2 W B 67 3 Z C 9 4 Z D 17 ,只需vector

DF$H