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
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
I believe you simply want vars[key]
.
I think intersect
could be useful to you...
vars[ intersect(key,names(vars)) ]
#b a
#2 1