[R] Creating variables on the fly
Ulrik Stervbo
ulrik.stervbo at gmail.com
Sat Apr 23 08:19:11 CEST 2016
Hi Georg,
The " around Kunden$* looks unintentional to me.
Second: haveyou considered using a long table? Then you would fill a known
set of columns.
Third if you must have columns based on year I believe df[[a.column.name]]
will work.
Best
Ulrik
<G.Maubach at gmx.de> schrieb am Sa., 23. Apr. 2016 07:33:
> 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 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