1

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.

Community
  • 1
  • 1
Gabriel Fair
  • 4,081
  • 5
  • 33
  • 54
  • It'd be easier if you make the input reproducible, ie. use `dput` or something similar so that we can easily copy-n-paste into R and have your example ready – DeanAttali Jun 10 '15 at 00:25
  • 1
    I don't understand the desired output here. Please define all variables to make your problem [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). I think you just want `x[[building]]<-` maybe? But i don't know what `x` is supposed to be here. Rather than asking to translate syntax, focus on describing the problem you want to solve in R. – MrFlick Jun 10 '15 at 00:34
  • Sorry for the confusion, I have replaced x[building] with ♠building. This is to symbolize the R functionality I am looking for. I am not looking for corrections to my grammar. I also cannot provide all my code as it is company code and having working code is irrelevant to the question. I am not looking for correct code. I am looking for an R equivalent of a feature I use in bash. – Gabriel Fair Jun 10 '15 at 00:57

2 Answers2

3

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.

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
0

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).

Olivier7121
  • 151
  • 1
  • 11