本文介绍了R:具有最大值的子集/组数据帧?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出如下数据框架:

  gid set  a  b
1   1   1  1  9
2   1   2 -2 -3
3   1   3  5  6
4   2   2 -4 -7
5   2   6  5 10
6   2   9  2  0

如何将唯一的 gid 的子集/组数据框与最大设置值和1/0以上的 a 值大于其 b value?

How can I subset/group data frame of a unique gid with the max set value and 1/0 wether its a value is greater than its b value?

所以这里,呃...呃...

So here, it'd be, uh...

1,3,0
2,9,1

SQL中的一个愚蠢的简单的东西,但是我想要更好地控制我的R,所以...

Kind of a stupid simple thing in SQL but I'd like to have a bit better control over my R, so...

推荐答案

蛋糕与 dplyr

dat <- read.table(text="gid set  a  b
1   1  1  9
1   2 -2 -3
1   3  5  6
2   2 -4 -7
2   6  5 10
2   9  2  0", header=TRUE)

library(dplyr)

dat %>%
  group_by(gid) %>%
  filter(row_number() == which.max(set)) %>%
  mutate(greater=a>b) %>%
  select(gid, set, greater)

## Source: local data frame [2 x 3]
## Groups: gid
##
##   gid set greater
## 1   1   3   FALSE
## 2   2   9    TRUE

如果您真的需要 1 0 dplyr em> groups 引起任何焦虑:

If you really need 1's and 0's and the dplyr groups cause any angst:

dat %>%
  group_by(gid) %>%
  filter(row_number() == which.max(set)) %>%
  mutate(greater=ifelse(a>b, 1, 0)) %>%
  select(gid, set, greater) %>%
  ungroup

## Source: local data frame [2 x 3]
##
##   gid set greater
## 1   1   3       0
## 2   2   9       1

你可以做同样的事情没有管道:

You could do the same thing without pipes:

ungroup(
  select(
    mutate(
      filter(row_number() == which.max(set)),
      greater=ifelse(a>b, 1, 0)), gid, set, greater))

但...但... 为什么? : - )

but…but… why?! :-)

这篇关于R:具有最大值的子集/组数据帧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 20:49
查看更多