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

Love Bohman love.bohman at sociology.su.se
Mon Dec 4 22:46:06 CET 2017

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.


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.)


> 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

More information about the R-help mailing list