Skip to content

Creating variables on the fly

4 messages · G.Maubach at gmx.de, Sarah Goslee, Ulrik Stervbo +1 more

#
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
#
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 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:

  
  
2 days later
#
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:
a
1 1
2 2
3 3
4 4
a  b
1 1  3
2 2  6
3 3  9
4 4 12