[R] Creating variables on the fly

Sarah Goslee sarah.goslee at gmail.com
Sat Apr 23 08:10:37 CEST 2016


The direct answer to your question is to look at ?get and ? assign.

The R-ish answer to your question is to store the data as elements of a
list rather than separate files and use lapply() instead.

Sarah

On Friday, April 22, 2016, <G.Maubach at gmx.de> wrote:

> Hi all,
>
> I would like to use a loop for tasks that occurs repeatedly:
>
> # Groups
> # Umsatz <= 0: 1 (NICHT kaufend)
> # Umsatz > 0: 2  (kaufend)
> for (year in c("2011", "2012", "2013", "2014", "2015")) {
>   paste0("Kunden$Kunde_real_", year) <- (paste0("Kunden$Umsatz_", year) <=
> 0) * 1 +
>                                         (paste0("Kunden$Umsatz_", year) >
> 0) * 2
>   paste0("Kunden$Kunde_real_", year) <- factor(paste0("Kunden$Umsatz_",
> year),
>                                                levels = c(1, 2),
>                                                labels = c("NICHT kaufend",
> "kaufend"))
>   }
>
> This actually does not work due to the fact that the expression
> "paste0("Kunden$Kunde_real_", year)" ist not interpreted as a variable name
> by the R script language interpreter.
>
> Is there a way to assembly variable names on the fly in R?
>
> Regards
>
> Georg
>
> ______________________________________________
> R-help at r-project.org <javascript:;> 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.
>


-- 
Sarah Goslee
http://www.stringpage.com
http://www.sarahgoslee.com
http://www.functionaldiversity.org

	[[alternative HTML version deleted]]



More information about the R-help mailing list