Skip to content

A smart way to use "$" in data frame

4 messages · Yuan, Rebecca, Duncan Murdoch, Greg Snow

#
On 18/01/2013 2:40 PM, Yuan, Rebecca wrote:
Yes, you could use the name of the dataframe to get the column names, e.g.

state <- c(dataa="oldstate", datab="newstate")
balance <- c(dataa="oldbalance", datab="newbalance")
dfname <- "dataa"
df <- get(dfname)
df[ df[,state[dfname]] == 'AR', balance[dfname]]

but that is really, really unreadable code.  You would be much better 
off to name the columns consistently in the first place.

Duncan Murdoch
3 days later