多列中具有最高值的子集行

时间:2016-06-28 21:16:03

标签: r

我有这样的问题:我想根据不同列中的最高值为每一行分配一个char值。我会试着更好地解释一下:

      col1 col2 col3 col4
row1  10   20   30
row2  20   10   10
row3  30   10   10

我想将col4作为因素分配给数据框的每一行的col1:col3间隔中具有最高值的列的名称。例如,row1我希望col4名称为“col3”,row2名称为“col1”,依此类推。

使用dplyr或其他基本功能在R中有一种简单的方法吗?

2 个答案:

答案 0 :(得分:3)

您实际上甚至不需要使用申请。你可以简单地完成一项任务:

package guru.springframework.controllers;

import guru.springframework.services.ProductService;
import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

import static org.assertj.core.api.Assertions.assertThat;

@RunWith(SpringRunner.class)
@WebMvcTest(ProductController.class)
public class ProductControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private ProductService productService;

    @Test
    public void testList() throws Exception {
      mockMvc.perform(MockMvcRequestBuilders.get("/products"))
                .andExpect(MockMvcResultMatchers.status().isOk())
                 .andExpect(MockMvcResultMatchers.view().name("products"))
                 .andExpect(MockMvcResultMatchers.model().attributeExists("products"))
               .andExpect(MockMvcResultMatchers.model().attribute("products",
                        Matchers.is(Matchers.empty())));

    }
}

这也会处理您在领带问题中选择的列。为了说明这是如何工作的,我们可以稍微修改原始数据:

df$col4 <- names(df)[max.col(df,ties.method="first")]

注意第一行col2和col3的值是多少。根据您对使用字母顺序第一列的评论,我们需要col2。

运行上面的代码,我们看到它按预期工作:

df <-read.table(header=T,text="   col1 col2 col3
  10   20   20
  20   10   10
  30   10   10")

修改:如果您的数据集中不仅有 col1 col2 col3 col4 1 10 20 20 col2 2 20 10 10 col1 3 30 10 10 col1 (可能还有其他变量,您不想占用最大值),那么更完整的解决方案将是指定要取最大值的列。像这样:

数据:

cols1 - cols3

然后您可以运行代码以查看其工作原理:

#Note this has an extra id variable on it
df <-read.table(header=T,text="   col1 col2 col3 id
  10   20   20 100
  20   10   10 100
  30   10   10 100")

答案 1 :(得分:0)

您可以使用which.maxnames(或colnames,如果这是一个矩阵)执行此操作:

# Data from example
dat <- data.frame(col1 = c(10, 20, 30), col2 = c(20, 10, 10), col3 = c(30, 10, 10))

dat$col4 <- apply(dat, 1, function(x) names(dat)[which.max(x)])
相关问题