This may seem trivial: ls() gives me names of variables as a character vector. How can I print the values 'behind' those character values: ls() > "a" > a [1] 5 How can I do e.g. print("unknown"(ls())) and get the variable values in my current environment? -- Christian W. Hoffmann CH - 8915 Hausen am Albis, Schweiz Rigiblickstrasse 15 b, Tel.+41-44-7640853 mailto: christian at echoffmann.ch home: www.echoffmann.ch
Hello, I'm not sure I understand, but something like this? a <- 5 get("a") See ?get Hope this helps, Rui Barradas ? Citando Christian Hoffmann <christian at echoffmann.ch>:> This may seem trivial: > > ls() gives me names of variables as a character vector. How can I print > the values 'behind' those character values: > > ls() >> "a" >> a > > [1] 5 > > How can I do e.g. print("unknown"(ls())) and get the variable values in > my current environment? > > -- > Christian W. Hoffmann > CH - 8915 Hausen am Albis, Schweiz > Rigiblickstrasse 15 b, Tel.+41-44-7640853 > mailto: christian at echoffmann.ch > home: www.echoffmann.ch[1] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.htmland provide commented, > minimal, self-contained, reproducible code.? Liga??es: --------- [1] http://www.echoffmann.ch [[alternative HTML version deleted]]
Is this what you want::> a <- 1 > b <- 2 > # get current objects > x <- ls() > # create list of values > my_list <- lapply(x, get) > # now add the names > names(my_list) <- x > my_list # print values$a [1] 1 $b [1] 2 Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it. On Sun, Oct 18, 2015 at 3:16 PM, <ruipbarradas at sapo.pt> wrote:> Hello, > > I'm not sure I understand, but something like this? > > a <- 5 > get("a") > > See ?get > Hope this helps, > > Rui Barradas > > > Citando Christian Hoffmann <christian at echoffmann.ch>: > > > This may seem trivial: > > > > ls() gives me names of variables as a character vector. How can I print > > the values 'behind' those character values: > > > > ls() > >> "a" > >> a > > > > [1] 5 > > > > How can I do e.g. print("unknown"(ls())) and get the variable values in > > my current environment? > > > > -- > > Christian W. Hoffmann > > CH - 8915 Hausen am Albis, Schweiz > > Rigiblickstrasse 15 b, Tel.+41-44-7640853 > > mailto: christian at echoffmann.ch > > home: www.echoffmann.ch[1] > > > > ______________________________________________ > > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide > > http://www.R-project.org/posting-guide.htmland provide commented, > > minimal, self-contained, reproducible code. > > > > Liga??es: > --------- > [1] http://www.echoffmann.ch > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
> On Oct 18, 2015, at 10:51 AM, Christian Hoffmann <christian at echoffmann.ch> wrote: > > How can I do e.g. print("unknown"(ls())) and get the variable values in my current environment?From your example, it sounds like what you want is simply this: sapply(ls(), get) James
On 19 Oct 2015, at 16:16 , James Toll <james at jtoll.com> wrote:> >> On Oct 18, 2015, at 10:51 AM, Christian Hoffmann <christian at echoffmann.ch> wrote: >> >> How can I do e.g. print("unknown"(ls())) and get the variable values in my current environment? > > From your example, it sounds like what you want is simply this: > > sapply(ls(), get) >Perhaps lapply rather than sapply; I don't think the simplification step of the latter is desirable. Incidentally, both variations are effectively crossing the creek to fetch as.list(GlobalEnv). If your environment contains large objects, you will get what you asked for, but that might well be more than you expected.> > James > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Office: A 4.23 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com