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

Love Bohman
Mon, 04 Dec 2017 11:33:25 -0000

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]]