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

peter dalgaard pdalgd at gmail.com
Mon Dec 4 16:38:52 CET 2017


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



More information about the R-help mailing list