我怎样才能提取出最好的"我数据框中每个子类别的行?

时间:2017-10-04 17:56:48

标签: r subset lapply

在下面的示例中,order函数返回的索引用于按以下方式对每个组中的条目进行排序:

set.seed(123)

ex.df <- data.frame(
  group = sample(LETTERS[1:4],20,replace=TRUE),
  score1 = sample(1:10),
  score2 = sample(1:10)
)

sortedOrderings <- by(ex.df, ex.df$group, function(df) order(df$score1 + df$score2) )

bestIndices <- lapply(sortedOrderings, FUN= function(lst) lst[1] )

问题是order看到数据框的索引由by而不是ex.df本身进行了子集化,因此使用它来从ex.df中提取相关行并不是最明智的想法:

print(sortedOrderings)

ex.df$group: A
[1] 2 3 4 1
--------------------------------------------------------------- 
ex.df$group: B
[1] 5 3 2 4 1
--------------------------------------------------------------- 
ex.df$group: C
[1] 2 1 3 4
--------------------------------------------------------------- 
ex.df$group: D
[1] 3 7 4 6 1 2 5

> print(ex.df[bestIndices,])
    group score1 score2
2       D      7      9
5       D      4      1
2.1     D      7      9
3       B      6      6

有没有办法拉出最好的&#34; ex.df中每个组的行,或者至少有索引引用ex.df

2 个答案:

答案 0 :(得分:1)

您可以使用dplyr包和rank功能。它看起来像这样:

ex.df %>%
  mutate(total_score = score1 + score2) %>%
  group_by(group) %>%
  mutate(rank = rank(total_score)) %>%
  filter(rank == max(rank)) %>%
  select(-c(rank)) %>%
  arrange(group)

并告诉你:

# A tibble: 4 x 4
# Groups:   group [4]
   group score1 score2 total_score
  <fctr>  <int>  <int>       <int>
1      A      8      3          11
2      B      9     10          19
3      C     10      8          18
4      D      9     10          19

答案 1 :(得分:1)

使用set.seed(123) ex.df <- data.frame( group = sample(LETTERS[1:4],20,replace=TRUE), score1 = sample(1:10), score2 = sample(1:10) ) library(data.table) setDT(ex.df) ex.df[ex.df[,.I[(score1 + score2) == max(score1 + score2)][1],by = .(group)]$V1][order(group)] 对第一行的索引执行自联接,其中总分等于组中的最高分数:

   group score1 score2
1:     A      8      3
2:     B      9     10
3:     C     10      8
4:     D      9     10

返回

Scanner scanner = new Scanner(System.in);
System.out.print("Square size: ");
int size = scanner.nextInt();

char[][] square = new char[size][size];                         //two-dimen array helps visualize square shape you want

for (int row=0; row<size; row++) {
    for (int col=0; col<size; col++) {
        if (row==0 || row == size-1 || col==0 || col==size-1) { //if border of square
            square[row][col] = '*';
        }
        else {                                                  //if inside square
            square[row][col] = '.';
        }
    }
}

for (char[] row : square) {
    System.out.print("\n");
    for (char col : row) {
        System.out.print(col);
    }
}