[R] Dynamic reference, right-hand side of function

William Dunlap wdunlap at tibco.com
Mon Dec 4 23:56:40 CET 2017


Note that in
    for (year in 2000:2007){
        varname <- paste0("aa_",year)
        assign(paste0(varname), as.vector(eval(as.name(varname))))
    }
the paste0(varname) is redundant - varname was just computed as the return
value of paste0().

People are trying to steer you towards making a list or environment that
contains
only your aa_XXXX objects because that will make subsequent code more
readable and maintainable.  (The initial setup can be weird because of the
string manipulations required.)

E.g., suppose you have datasets aa_XXXX for some arbitrary set of years.  If
you did not create a list of them when reading in the data
    aa_1970 <- scan(quiet=TRUE, text="2 3 5 8 10")
    aa_1981 <- scan(quiet=TRUE, text="11 17 18 19 23")
then you can package all of them in the current environment into a list with
    aa_names <- objects(pattern="^aa_[[:digit:]]{4}$")
    names(aa_names) <- sub("^aa_", "", aa_names) # just the year
    aa_list <- lapply(aa_names, get)
Once you have a list of all your datasets then you can do operations on all
the
elements of the list without having to remember which years you have data
for.
    aa_list <- lapply(aa_list, as.numeric)
    aa_trends <- lapply(aa_list, function(x) { iota <- seq_along(x) ;
coef(lm(x ~ iota)) })

To read in data from a bunch of files called aa_XXXX.txt, making the names
of
the list be the XXXX part of the file name, do
    aa_files <- dir(pattern="^aa_[[:digit:]]{4}\\.txt$")
    names(aa_files) <- sub("aa_([[:digit:]]{4})\\.txt$", "\\1", aa_files)
    aa_list <- lapply(aa_files, readAnAAFile)
where 'readAnAAFile' is the function you use to read one such file.


Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Mon, Dec 4, 2017 at 1:46 PM, Love Bohman <love.bohman at sociology.su.se>
wrote:

> Hi!
> Thanks for the replies!
> I understand people more accustomed to R doesn't like looping much, and
> that thinking about loops is something I do since I worked with Stata a
> lot. The syntax from Peter Dalgaard was really clever, and I learned a lot
> from it, even though it didn't solve my problem (I guess it wasn't very
> well explained). My problem was basically that I have a data matrix
> consisting of just 1 row, and I want to convert that row into a vector.
> However, when trying to do that dynamically, I couldn't get R to read the
> right hand side of the syntax as a variable name instead of a string.
> However, together with a colleague I finally solved it with the (eval(
> as.name()) function (I include the loop I used below). I understand that
> looping isn't kosher among you more devoted R-users, and eventually I hope
> I will learn to use lists in the future instead.
>
> Thanks!
> Love
>
>
> for (year in 2000:2007){
> varname <- paste0("aa_",year)
> assign(paste0(varname), as.vector(eval(as.name(varname))))
> }
>
> -----Ursprungligt meddelande-----
> Från: peter dalgaard [mailto:pdalgd at gmail.com]
> Skickat: den 4 december 2017 16:39
> Till: Love Bohman <love.bohman at sociology.su.se>
> Kopia: r-help at r-project.org
> Ämne: Re: [R] Dynamic reference, right-hand side of function
>
> The generic rule is that R is not a macro language, so looping of names of
> things gets awkward. It is usually easier to use compound objects like
> lists and iterate over them. E.g.
>
> datanames <- paste0("aa_", 2000:2007)
> datalist <- lapply(datanames, get)
> names(datalist) <- datanames
> col1 <- lapply(datalist, "[[", 1)
> colnum <- lapply(col1, as.numeric)
>
> (The 2nd line assumes that the damage has already been done so that you
> have aa_2000 ... aa_2007 in your workspace. You might alternatively create
> the list directly while importing the data.)
>
> -pd
>
> > On 4 Dec 2017, at 12:33 , Love Bohman <love.bohman at sociology.su.se>
> wrote:
> >
> > Hi R-users!
> > Being new to R, and a fairly advanced Stata-user, I guess part of my
> problem is that my mindset (and probably my language as well) is wrong.
> Anyway, I have what I guess is a rather simple problem, that I now without
> success spent days trying to solve.
> >
> > I have a bunch of datasets imported from Stata that is labelled aa_2000
> aa_2001 aa_2002, etc. Each dataset is imported as a matrix, and consists of
> one column only. The columns consists of integer numbers. I need to convert
> the data to vectors, which I found several ways to do. I use, for example:
> > aa_2000 <- as.numeric(aa_2000[,1])
> > However, when trying to automate the task, so I don't have to write a
> line of code for each dataset, I get stuck. Since I'm a Stata user, my
> first attempt is trying to make a loop in order to loop over all datasets.
> However, I manage to write a loop that works for the left-hand side of the
> syntax, but not for the right-hand side.
> > I have included some examples from my struggles to solve the issue
> below, what they all have in common is that I don't manage to call for any
> "macro" (is that only a Stata-word?) in the right hand side of the
> functions. When I try to replace the static reference with a dynamic one
> (like in the left-hand side), the syntax just doesn't work.
> >
> > I would very much appreciate some help with this issue!
> > All the best,
> > Love
> >
> > year <- 2002
> > dataname <- paste0("aa_",year)
> > assign(paste0(dataname), as.numeric(aa_2002[,1]))
> >
> > year <- 2003
> > assign(paste0("aa_",year), as.numeric(aa_2003))
> >
> > year <- 2005
> > assign(paste0("aa_",year), aa_2005[,1])
> >
> > list1 <- c(2000:2007)
> > list1[c(7)]
> > assign(paste0("aa_",list1[c(7)]), as.numeric(paste0(aa_2006)))
> >
> >
> >       [[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.
>
> --
> 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
>
> ______________________________________________
> 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]]



More information about the R-help mailing list