R 如何按第n个最高值汇总列数据并在新df中获取日期
我有这个数据集R 如何按第n个最高值汇总列数据并在新df中获取日期,r,R,我有这个数据集 date<-as.Date(c("2007-01-01","2007-01-02","2007-01-03","2007-01-04","2007-01-05")) a<-c(55,8,3,7,126) b<-c(3,199,7,66,8) c<-c(91,333,2,9,4) df<-data.frame(date,a,b,c) date a b c 2007-01-01 55 3 91 2007-01-02
date<-as.Date(c("2007-01-01","2007-01-02","2007-01-03","2007-01-04","2007-01-05"))
a<-c(55,8,3,7,126)
b<-c(3,199,7,66,8)
c<-c(91,333,2,9,4)
df<-data.frame(date,a,b,c)
date a b c
2007-01-01 55 3 91
2007-01-02 8 199 333
2007-01-03 3 7 2
2007-01-04 7 66 9
2007-01-05 126 8 4
我在dplyr、tidyr、apply函数中尝试了许多不同的动词,但我真的无法接近。请帮助,谢谢。date%#获取第二行(第二高值)
date<-as.Date(c("2007-01-01","2007-01-02","2007-01-03","2007-01-04","2007-01-05"))
a<-c(55,8,3,7,126)
b<-c(3,199,7,66,8)
c<-c(91,333,2,9,4)
df<-data.frame(date,a,b,c)
library(tidyverse)
df %>%
gather(Type,value,-date) %>% # reshape dataset
arrange(desc(value)) %>% # arrange in descending order
group_by(Type) %>% # for each type
slice(2) %>% # get 2nd row (2nd highest value)
ungroup() # forget the grouping
# # A tibble: 3 x 3
# date Type value
# <date> <chr> <dbl>
# 1 2007-01-01 a 55
# 2 2007-01-04 b 66
# 3 2007-01-01 c 91
取消分组()#忘记分组
##tibble:3 x 3
#日期类型值
#
#1 2007-01-01 a 55
#2 2007-01-04 b 66
#3 2007-01-01 c 91
date%#获取第二行(第二高值)
取消分组()#忘记分组
##tibble:3 x 3
#日期类型值
#
#1 2007-01-01 a 55
#2 2007-01-04 b 66
#3 2007-01-01 c 91
您也可以使用nth
(forking AntoniosK的解决方案):
库(tidyverse)
df%>%
聚集(类型、值、日期)%>%#重塑数据集
按(类型)%>%对每种类型进行分组
筛选器(值==n(值,2,-值))%>%
解组
##tibble:3 x 3
#日期类型值
#
#1 2007-01-01 a 55
#2 2007-01-04 b 66
#3 2007-01-01 c 91
和基本的R解决方案:
library(tidyverse)
df %>%
gather(Type,value,-date) %>% # reshape dataset
group_by(Type) %>% # for each type
filter(value==nth(value,2,-value)) %>%
ungroup
# # A tibble: 3 x 3
# date Type value
# <date> <chr> <dbl>
# 1 2007-01-01 a 55
# 2 2007-01-04 b 66
# 3 2007-01-01 c 91
pos <- sapply(df[-1],function(x) which(rank(-x)==2))
rows <- lapply(1:3,function(x)
setNames(transform(df[pos[x],c(1,1+x)],Type=names(pos)[x]),c("date","value","type")))
do.call(rbind,rows)
# date value type
# 1 2007-01-01 55 a
# 4 2007-01-04 66 b
# 11 2007-01-01 91 c
pos您也可以使用nth
(forking AntoniosK的解决方案):
库(tidyverse)
df%>%
聚集(类型、值、日期)%>%#重塑数据集
按(类型)%>%对每种类型进行分组
筛选器(值==n(值,2,-值))%>%
解组
##tibble:3 x 3
#日期类型值
#
#1 2007-01-01 a 55
#2 2007-01-04 b 66
#3 2007-01-01 c 91
和基本的R解决方案:
library(tidyverse)
df %>%
gather(Type,value,-date) %>% # reshape dataset
group_by(Type) %>% # for each type
filter(value==nth(value,2,-value)) %>%
ungroup
# # A tibble: 3 x 3
# date Type value
# <date> <chr> <dbl>
# 1 2007-01-01 a 55
# 2 2007-01-04 b 66
# 3 2007-01-01 c 91
pos <- sapply(df[-1],function(x) which(rank(-x)==2))
rows <- lapply(1:3,function(x)
setNames(transform(df[pos[x],c(1,1+x)],Type=names(pos)[x]),c("date","value","type")))
do.call(rbind,rows)
# date value type
# 1 2007-01-01 55 a
# 4 2007-01-04 66 b
# 11 2007-01-01 91 c
pos哇,非常感谢你们两位的深刻见解。哇,非常感谢你们两位的深刻见解。