gpt4 book ai didi

按组排序变量 (dplyr)

转载 作者:行者123 更新时间:2023-12-03 13:29:26 29 4
gpt4 key购买 nike

我有一个带列的数据框 x1, x2, group我想生成一个带有额外列的新数据框 rank表示x1的顺序在其组中。

有相关问题here ,但已接受的答案似乎不再有效。

到这里为止,很好:

library(dplyr)
data(iris)
by_species <- iris %>%
arrange(Species, Sepal.Length) %>%
group_by(Species)

但是当我尝试按组获得排名时:
by_species <- mutate(by_species, rank=row_number())

错误是:

Error in rank(x, ties.method = "first", na.last = "keep") :
argument "x" is missing, with no default



更新

问题是 dplyr 之间存在冲突和 plyr .要重现错误,请加载两个包:
library(dplyr)
library(plyr)
data(iris)
by_species <- iris %>%
arrange(Species, Sepal.Length) %>%
group_by(Species) %>%
mutate(rank=row_number())
# Error in rank(x, ties.method = "first", na.last = "keep") :
# argument "x" is missing, with no default

卸货 plyr它可以正常工作:
detach("package:plyr", unload=TRUE)
by_species <- iris %>%
arrange(Species, Sepal.Length) %>%
group_by(Species) %>%
mutate(rank=row_number())

by_species %>% filter(rank <= 3)

## Sepal.Length Sepal.Width Petal.Length Petal.Width Species rank
## (dbl) (dbl) (dbl) (dbl) (fctr) (int)
## 1 4.3 3.0 1.1 0.1 setosa 1
## 2 4.4 2.9 1.4 0.2 setosa 2
## 3 4.4 3.0 1.3 0.2 setosa 3
## 4 4.9 2.4 3.3 1.0 versicolor 1
## 5 5.0 2.0 3.5 1.0 versicolor 2
## 6 5.0 2.3 3.3 1.0 versicolor 3
## 7 4.9 2.5 4.5 1.7 virginica 1
## 8 5.6 2.8 4.9 2.0 virginica 2
## 9 5.7 2.5 5.0 2.0 virginica 3

最佳答案

以下生成指定的所需结果。

library(dplyr)

by_species <- iris %>% arrange(Species, Sepal.Length) %>%
group_by(Species) %>%
mutate(rank = rank(Sepal.Length, ties.method = "first"))

by_species %>% filter(rank <= 3)
##Source: local data frame [9 x 6]
##Groups: Species [3]
##
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species rank
## (dbl) (dbl) (dbl) (dbl) (fctr) (int)
##1 4.3 3.0 1.1 0.1 setosa 1
##2 4.4 2.9 1.4 0.2 setosa 2
##3 4.4 3.0 1.3 0.2 setosa 3
##4 4.9 2.4 3.3 1.0 versicolor 1
##5 5.0 2.0 3.5 1.0 versicolor 2
##6 5.0 2.3 3.3 1.0 versicolor 3
##7 4.9 2.5 4.5 1.7 virginica 1
##8 5.6 2.8 4.9 2.0 virginica 2
##9 5.7 2.5 5.0 2.0 virginica 3

by_species %>% slice(1:3)
##Source: local data frame [9 x 6]
##Groups: Species [3]
##
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species rank
## (dbl) (dbl) (dbl) (dbl) (fctr) (int)
##1 4.3 3.0 1.1 0.1 setosa 1
##2 4.4 2.9 1.4 0.2 setosa 2
##3 4.4 3.0 1.3 0.2 setosa 3
##4 4.9 2.4 3.3 1.0 versicolor 1
##5 5.0 2.0 3.5 1.0 versicolor 2
##6 5.0 2.3 3.3 1.0 versicolor 3
##7 4.9 2.5 4.5 1.7 virginica 1
##8 5.6 2.8 4.9 2.0 virginica 2
##9 5.7 2.5 5.0 2.0 virginica 3

关于按组排序变量 (dplyr),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34967837/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com