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 think
intersect
could be useful to you...I believe you simply want
vars[key]
.