如何使用R在数据框的列中查找前n%个记录 [英] How to find top n% of records in a column of a dataframe using R

查看:35
本文介绍了如何使用R在数据框的列中查找前n%个记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据集,显示了大约20年内每天一次的澳元兑美元的汇率.我将数据存储在数据框中,第一列是日期,第二列是汇率.这是来自数据的示例:

I have a dataset showing the exchange rate of the Australian Dollar versus the US dollar once a day over a period of about 20 years. I have the data in a data frame, with the first column being the date, and the second column being the exchange rate. Here's a sample from the data:

>data
             V1     V2
1    12/12/1983 0.9175
2    13/12/1983 0.9010
3    14/12/1983 0.9000
4    15/12/1983 0.8978
5    16/12/1983 0.8928
6    19/12/1983 0.8770
7    20/12/1983 0.8795
8    21/12/1983 0.8905
9    22/12/1983 0.9005
10   23/12/1983 0.9005

我将如何显示这些记录中的前n%个?例如.说我想查看汇率落入数据集中所有汇率的前5%的那几天和汇率?

How would I go about displaying the top n% of these records? E.g. say I want to see the days and exchange rates for those days where the exchange rate falls in the top 5% of all exchange rates in the dataset?

推荐答案

对于前5%的用户:

n <- 5
data[data$V2 > quantile(data$V2,prob=1-n/100),]

这篇关于如何使用R在数据框的列中查找前n%个记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆