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

Ek Esawi esawiek at gmail.com
Mon Dec 4 15:47:14 CET 2017

Hi Love,

I am not sure if I understand your question and it will help if you
provided a sample data frame, sample of your code and sample of your output
and the output you desire. Having said that, I think you could use cbind to
join all datasets into a matrix and use the apply family of functions to
achieve what you are after.

On Mon, Dec 4, 2017 at 6:33 AM, Love Bohman <love.bohman at sociology.su.se>

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

	[[alternative HTML version deleted]]

More information about the R-help mailing list