gpt4 book ai didi

R - 如何在保持分组的同时重新排列数据框中的行?

转载 作者:行者123 更新时间:2023-12-03 14:06:22 24 4
gpt4 key购买 nike

我有以下数据框。这些行目前按物种组排序,即高粱组后跟禾本科组。

# create a dataset
specie <- c(rep("sorgho" , 3) , rep("poacee" , 3) , rep("banana" , 3) , rep("triticum" , 3) )
condition <- rep(c("normal" , "stress" , "Nitrogen") , 4)
value <- abs(rnorm(12 , 0 , 15))
data <- data.frame(specie,condition,value)
print(data)

specie condition value
1 sorgho normal 12.623696
2 sorgho stress 11.394047
3 sorgho Nitrogen 0.498003
4 poacee normal 14.589322
5 poacee stress 10.744153
6 poacee Nitrogen 7.299742
7 banana normal 9.845850
8 banana stress 9.416088
9 banana Nitrogen 4.178521
10 triticum normal 13.230663
11 triticum stress 30.658355
12 triticum Nitrogen 9.402721
我怎样才能重新排列这些分组,使它们按氮值递减的顺序排列?我希望将数据框重新组织为类似于以下内容:
     specie condition     value
10 triticum normal 13.230663
11 triticum stress 30.658355
12 triticum Nitrogen 9.402721
4 poacee normal 14.589322
5 poacee stress 10.744153
6 poacee Nitrogen 7.299742
7 banana normal 9.845850
8 banana stress 9.416088
9 banana Nitrogen 4.178521
1 sorgho normal 12.623696
2 sorgho stress 11.394047
3 sorgho Nitrogen 0.498003

最佳答案

我们可以filter “氮”行,arrange 'value' 按降序排列,提取 'specie' 并将其用作 levels '安排'specie'栏

library(dplyr)
lvls <- data %>%
filter(condition == 'Nitrogen') %>%
arrange(desc(value)) %>%
pull(specie)
data %>%
arrange(factor(specie, levels = lvls))%>%
as_tibble
-输出
# A tibble: 12 x 3
# specie condition value
# <chr> <chr> <dbl>
# 1 triticum normal 13.2
# 2 triticum stress 30.7
# 3 triticum Nitrogen 9.40
# 4 poacee normal 14.6
# 5 poacee stress 10.7
# 6 poacee Nitrogen 7.30
# 7 banana normal 9.85
# 8 banana stress 9.42
# 9 banana Nitrogen 4.18
#10 sorgho normal 12.6
#11 sorgho stress 11.4
#12 sorgho Nitrogen 0.498

或者在单个管道中执行此操作
data %>%
arrange(factor(specie, levels =
unique(specie)[order(-value[condition == 'Nitrogen'])]))

或使用 base R
data[order(with(data, factor(specie, levels = 
unique(specie)[order(-value[condition == "Nitrogen"])]))),]
数据
data <- structure(list(specie = c("sorgho", "sorgho", "sorgho", "poacee", 
"poacee", "poacee", "banana", "banana", "banana", "triticum",
"triticum", "triticum"), condition = c("normal", "stress", "Nitrogen",
"normal", "stress", "Nitrogen", "normal", "stress", "Nitrogen",
"normal", "stress", "Nitrogen"), value = c(12.623696, 11.394047,
0.498003, 14.589322, 10.744153, 7.299742, 9.84585, 9.416088,
4.178521, 13.230663, 30.658355, 9.402721)), class = "data.frame",
row.names = c("1",
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"))

关于R - 如何在保持分组的同时重新排列数据框中的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65135836/

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