library(dplyr)
## Warning: package 'dplyr' was built under R version 3.2.5
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(base)
deej<-read.csv("C:/data scientist/DEEJ Market Price.csv",header = T)
deejorder<-deej[order(deej$收盘价.元.,decreasing = T),]
head(deejorder)
## 日期 开盘价.元. 最高价.元. 最低价.元. 收盘价.元. 成交额.百万.
## 4701 2016/8/23 58.42 63.63 58.42 62.10 1598.34
## 4398 2015/6/1 58.00 62.38 58.00 61.80 1935.42
## 4408 2015/6/15 60.08 63.42 59.52 61.70 2065.62
## 4399 2015/6/2 61.80 62.57 59.88 61.62 1682.39
## 4702 2016/8/24 62.45 62.60 60.50 61.24 934.28
## 4405 2015/6/10 60.22 61.86 59.25 60.75 1300.05
## 成交量
## 4701 25976300
## 4398 31992100
## 4408 33327900
## 4399 27518700
## 4702 15272600
## 4405 21500900
sort(unique(deej$收盘价.元.),decreasing = T,na.last = NA)[2]
## [1] 61.8
arrange(deej, desc(收盘价.元.))[1:2,]
## 日期 开盘价.元. 最高价.元. 最低价.元. 收盘价.元. 成交额.百万.
## 1 2016/8/23 58.42 63.63 58.42 62.1 1598.34
## 2 2015/6/1 58.00 62.38 58.00 61.8 1935.42
## 成交量
## 1 25976300
## 2 31992100
proc.time()
## user system elapsed
## 1.18 0.15 1.89
max(deej$收盘价.元.,na.rm = T) ## 第一大
## [1] 62.1
a<-deej$收盘价.元.[deej$收盘价.元. != max(deej$收盘价.元.,na.rm = T)]
max(a,na.rm = T) ## 第二大
## [1] 61.8
proc.time()
## user system elapsed
## 1.23 0.17 1.95
deej %>% top_n(2,收盘价.元.) %>% arrange (desc(收盘价.元.))
## 日期 开盘价.元. 最高价.元. 最低价.元. 收盘价.元. 成交额.百万.
## 1 2016/8/23 58.42 63.63 58.42 62.1 1598.34
## 2 2015/6/1 58.00 62.38 58.00 61.8 1935.42
## 成交量
## 1 25976300
## 2 31992100
proc.time()
## user system elapsed
## 1.25 0.20 2.00
综上确实是方法三最快,我想方法三应该是段老师你想要的效果
a<-data.frame(deej$收盘价.元.) %>% unique() %>% top_n(2)
## Selecting by deej.收盘价.元.
sort(a$deej.收盘价.元.,decreasing = T)
## [1] 62.1 61.8
proc.time()
## user system elapsed
## 1.29 0.21 2.06