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

G.Maubach at weinwolf.de G.Maubach at weinwolf.de
Tue Apr 26 08:44:44 CEST 2016


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





> Gesendet: Montag, 25. April 2016 um 19:35 Uhr
> Von: "MacQueen, Don" <macqueen1 at llnl.gov>
> An: "G.Maubach at gmx.de" <G.Maubach at gmx.de>, "r-help at r-project.org" 
<r-help at r-project.org>
> Betreff: Re: [R] Creating variables on the fly
>
> I'm going to assume that Kunden is a data frame, and it has columns
> (variables) with names like
>   Umstatz_2011
> and that you want to create new columns with names like
>   Kunde_real_2011
> 
> If that is so, then try this (not tested):
> 
> for (year in 2011:2015) {
>   nmK <- paste0("Kunde_real_", year)
>   nmU <- paste0("Umsatz_", year)
>   cat('Creating',nmK,'from',nmU,'\n')
>   Kunden[[ nmK ]] <- ifelse( Kunden[[ nmU ]] <= 0, 1, 2)
>   Kunden[[ nmK ]] <- factor( Kunden[[ nmK ]],
>        levels=c(1,2),
>        labels= c("NICHT kaufend", "kaufend")
>        )
> 
> }
> 
> This little example should illustrate the method:
> 
> 
> > foo <- data.frame(a=1:4)
> > foo
>   a
> 1 1
> 2 2
> 3 3
> 4 4
> > foo[['b']] <- foo[['a']]*3
> > foo
>   a  b
> 1 1  3
> 2 2  6
> 3 3  9
> 4 4 12
> 
> 
> 
> -- 
> Don MacQueen
> 
> Lawrence Livermore National Laboratory
> 7000 East Ave., L-627
> Livermore, CA 94550
> 925-423-1062
> 
> 
> 
> 
> 
> On 4/22/16, 8:52 AM, "R-help on behalf of G.Maubach at gmx.de"
> <r-help-bounces at r-project.org on behalf of 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 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.
> 
>



More information about the R-help mailing list