通过键返回值 [英] Return value by key

查看:84
本文介绍了通过键返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有更短的方法按键(由key元素给定)按顺序返回值?

Is there a shorter way to return the values by key in the order, which is given by elements of key?

vars<-c("a"=1,"b"=2)
key<-c("b","a")
ret<-c()
for(k in key)
ret<-c(ret,vars[names(vars) %in% k])
ret

推荐答案

我认为intersect对您可能有用...

I think intersect could be useful to you...

vars[ intersect(key,names(vars)) ]
#b a 
#2 1

这篇关于通过键返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆