计算数据子集的分位数

时间:2017-12-12 03:34:16

标签: r

我有一个像这样的数据框:

set.seed(2)    
df<- data.frame(region= c(rep(1,4), rep(2,4)),scale=sample(-1:4,8,replace=TRUE))

我希望按region对数据进行分组,然后计算该子集的分位数,并将25%分位数,75%分位数的结果放在单独的列中。

quantile(df[1:4,2])[2] #region 1 25% quantile
quantile(df[1:4,2])[4] #region 1 75% quantile
quantile(df[5:8,2])[2] #region 2 25% quantile
quantile(df[5:8,2])[4] #region 2 75% quantile

预期输出为:

output<- data.frame( region= c(1,2), Q1= c(0,2.75), Q3= c(2.25, 4))

我试过了:

out <- 
bos %>%
group_by(region)%>%
summarise(mean=mean(res_vec), sd= sd(res_vec), 
median=median(res_vec), mode= mode(res_vec),
        quantile1= quantile(scale, probs= 0.25),
        quantile2= quantile(scale, probs= 0.75))

AND  

quantiles<-aggregate(x = bos, by = list(bos$scale), fun = quantiles)

1 个答案:

答案 0 :(得分:2)

这就是你要找的东西?

class LineChart extends React.Component {
  getNativeComponentName() {
    return 'RNLineChart'
  }

  getNativeComponentRef() {
    return this.nativeComponentRef
  }

  render() {
    return <RNLineChart {...this.props} ref={ref => this.nativeComponentRef = ref} />;
  }
}

LineChart.propTypes = {
  ...BarLineChartBase.propTypes,

  data: lineData,
};

var RNLineChart = requireNativeComponent('RNLineChart', LineChart, {
  nativeOnly: {onSelect: true}
});

export default MoveEnhancer(LineChart);




class BarChart extends React.Component {
  getNativeComponentName() {
    return 'RNBarChart'
  }

  getNativeComponentRef() {
    return this.nativeComponentRef
  }

  render() {
    return <RNBarChart {...this.props} ref={ref => this.nativeComponentRef = ref} />;
  }
}

BarChart.propTypes = {
  ...BarLineChartBase.propTypes,

  drawValueAboveBar: PropTypes.bool,
  drawBarShadow: PropTypes.bool,

  data: barData
}

var RNBarChart = requireNativeComponent('RNBarChart', BarChart, {
  nativeOnly: {onSelect: true}
})

export default MoveEnhancer(BarChart)




export default function MoveEnhancer(Chart) {
  return class MovableExtended extends Chart {
    moveViewToX(x) {
      UIManager.dispatchViewManagerCommand(
        findNodeHandle(this.getNativeComponentRef()),
        UIManager[this.getNativeComponentName()].Commands.moveViewToX,
        [x]
      );
    }
  }
}