Skip to content
Prev 360422 / 398503 Next

Antwort: Fw: Re: Creating variables on the fly (SOLVED)

Hi Don,
Hi to all readers,

many thanks for all your answers and all your help.

I adapted Don's code to my data and Don's code does the trick:

str(Kunden01)

for (year in 2011:2015) {
  Reeller_Kunde <- paste0("Reeller_Kunde_", year)
  Umsatz <- paste0("Umsatz_", year)
  cat('Creating', Reeller_Kunde,'from', Umsatz,'\n')
  Kunden01[[ Reeller_Kunde ]] <- ifelse( Kunden01[[ Umsatz ]] >= 0, 1, 2)
  Kunden01[[ Reeller_Kunde ]] <- factor( Kunden01[[ Reeller_Kunde ]],
                                         levels=c(1,2),
                                         labels= c("NICHT kaufend", 
"kaufend")
  )
}

str(Kunden01)

This way a new variable is created by building it from a string 
concatenation.

I also like the cat() function to document the process within the loop 
while running the program.

Many thanks for your help.

Kind regards

Georg




Von:    G.Maubach at gmx.de
An:     g.maubach at weinwolf.de, 
Datum:  25.04.2016 21:37
Betreff:        Fw: Re: [R] Creating variables on the fly
<r-help at r-project.org>
Message-ID: <OF11F58074.BAA6E2AE-ONC1257FA1.00244C1F-C1257FA1.00250E41@lotus.hawesko.de>
In-Reply-To: <trinity-c113f29b-55d5-4bed-bd55-e03d3ac2ec98-1461613170058@3capp-gmx-bs01>