0

I am new to R language, if I use us_stocks$"LNC" I could get the corresponding data zoo. resB is a list with following elements.The library is zoo, which is the type of us_stocks

resB
# [[1]] LNC   7 
# [[2]] GAM  62 
# [[3]] CMA  7 

class(resB)
# [1] "list"

names(resB[[1]])
# [1] "LNC"

but when use us_stocks$names(resB[[1]]) I could not get the zoo series? How to fix this?

MrFlick
  • 195,160
  • 17
  • 277
  • 295
printemp
  • 869
  • 1
  • 10
  • 33
  • 1
    For that, you'll want double brackets `us_stocks[[names(resB[[1]])]]` – Frank Nov 06 '15 at 20:31
  • 1
    This is a good resource on flavours of subsetting: https://www.stat.auckland.ac.nz/~paul/ItDT/HTML/node65.html – Chris Nov 06 '15 at 20:31
  • also: https://cran.r-project.org/doc/manuals/R-lang.html#Indexing. Quote: *The form using $ applies to recursive objects such as lists and pairlists. It allows only a literal character string or a symbol as the index. That is, the index is not computable* – Chris Nov 06 '15 at 20:34
  • @Frank, does not work, thanks – printemp Nov 06 '15 at 20:36
  • Hm, well then I'm stumped. – Frank Nov 06 '15 at 20:37
  • 1
    If `resB <- list(LNC = 7, GAM = 62, CMA = 9); us_stocks <- data.frame(LNC = 1:3, GAM = 4:6, CMA = 7:9, IBM = 10:12)` then try: `us_stocks[names(resB)] ` Next time please use `dput` to provide reproducible code that generates minimal versions of your data exactly. – G. Grothendieck Nov 06 '15 at 21:00

1 Answers1

2

It often takes a while to understand what is meant by " ... $ is a function which does not evaluate its second argument." Most R functions would take names(resB[[1]]) and eval;uate it and then act on the value. But not $. It expects the second argument to be an actual column name but given as an unquoted string. This is an example of "non-standard evaluation". You will also see it operating in the functions library and help, as well as many functions in what is known perhaps flippantly as the hadleyverse, which includes the packages 'ggplot2' and 'dplyr'. The names of dataframe columns or the nodes of R lists are character literals, however, they are not really R names in the sense that their values cannot be accessed with an unquoted sequence of letters typed to the console at the toplevel of R.

So as already stated you should be using d[[ names(resB[[1]]) ]]. This is also much safer to use in programming, since there are often problems with scoping involved with the use of the $-function in anything other than interactive console use.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • What kind of scoping problems are you referring to in your last sentence? I've never had any with `$`. So, I'm curious. – Roland Nov 06 '15 at 20:52
  • It's possible that I have conflated issues with `$` which often causes difficulties with passing arguments into functions with the potential scoping difficulties using `subset` and `with`. – IRTFM Nov 06 '15 at 23:26
  • Yeah, doing anything fancy with `$` is trouble, I've found, as we discussed here: http://stackoverflow.com/questions/18216084/lapply-ing-with-the-function#comment26703667_18216084 – Frank Nov 07 '15 at 00:02