gpt4 book ai didi

r - 从命名向量中提取时保持顺序不变

转载 作者:行者123 更新时间:2023-12-03 21:33:31 24 4
gpt4 key购买 nike

所以如果我有:

>  g<-c(1,5,2,4,6)
> names(g)<-c("josh","daniel","john", "luke", "bill")
> g
josh daniel john luke bill
1 5 2 4 6
>
> g[c("john", "daniel", "bill")]
john daniel bill
2 5 6

是否可以在不使用排序函数的情况下返回最初在 g 中排序的值,即(daniel then john then bill)?

谢谢!
-乔希

最佳答案

%in%会为你做这件事:

> g[names(g) %in% c("john", "daniel", "bill")]
daniel john bill
5 2 6

关于r - 从命名向量中提取时保持顺序不变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7758520/

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