Does R support indirect variable expansion or variable substitution?

1.1k views Asked by At

In bash one can use an exclamation mark to use a variable's value as a variable. See explanation here. This is known as variable indirect expansion. This can be used to used to name a new variable using another variable in your code. I was wondering if this could be done in R lang.

For example, lets say I want to create a data frame of employees located at each of a companies' buildings.

head(talent_by_building)

   employee building
1    345618 Pi
2    195871 E
3    247274 Pi
4    929771 Pi
5    873096 E
6    665857 E
7    791656 E
8    133673 E
9    574058 C
10   208041 C
11   402100 C
12   167792 C
13   156971 C

And let "♠building" be the variable I want to indirectly expand. So I want to use the building name as a new variable of that building's name. I am using a ♠ character here because using ! (as it is used infront of a variable in bash) was causing some confusion.

completed_list <- c("") #Clear out vector
for(building in talent_by_building$building){

  if(!(building %in% completed_list)){
    ♠building<-talent_by_building[talent_by_building$building %in% building,]
  }
  append(completed_list,building)
}

If this was possible in R, the expected output would be the creation of three new data frames named for each of the buildings the for loop found. Pi,E,and C:

head(pi)

   employee building
1    345618 Pi
2    247274 Pi
3    929771 Pi


head(E)

   employee building
1    195871 E
2    873096 E
3    665857 E
4    791656 E
5    133673 E

head(C)

   employee building
1   574058 C
2   208041 C
3   402100 C
4   167792 C
5   156971 C

Is there a way to use the building name as the name of the new value? So in place of ♠building the value of the variable would be substituted as the variable name. I can do this in bash and was wondering if this was possible in R.

2

There are 2 answers

2
Ben Bolker On BEST ANSWER

tl;dr no, there is no short-hand syntax for variable substitution. Slightly longer answer: it's pretty easy to get your desired output. If you're going to be working with R for more than a very short time, it's probably worth learning some R idioms, though.

Construct example data:

talent_by_building <-  read.table(header=TRUE,text=
"employee building
345618 Pi
195871 E
247274 Pi
929771 Pi
873096 E
665857 E
791656 E
133673 E
574058 C
208041 C
402100 C
167792 C
156971 C")

First split the data by building:

ss <- split(talent_by_building,talent_by_building$building)

The idiomatic R way to deal with these data would be to leave them inside the list rather than creating new variables to clutter up the global workspace. But if you want to:

for (i in names(ss)) {
    assign(i,ss[[i]])
}
ls()

A more direct translation of your code:

BUILDING <- "Pi"
assign(BUILDING,subset(talent_by_building,building==BUILDING))

If you want to do something other than assignment you can use eval(), substitute(), and/or parse, but it's usually more trouble than it's worth.

0
Olivier7121 On

The functions 1) expression, quote and bquote and 2) eval might be what you are looking for (some kind of indirection if I correctly understood).