1

Is there any way of skipping data frame name all the time whenever I use a column name?

Example: lmc[lmc$BranchID=="NULL",] Instead I want to write like

     lmc[BranchID=="NULL",]
Clemsang
  • 5,053
  • 3
  • 23
  • 41
Gomes
  • 36
  • 6

1 Answers1

0

You can use attach:

df <- data.frame(BranchID = c(1, "NULL", "hey"), value = 1:3, stringsAsFactors = FALSE)
attach(df)
df[BranchID == "NULL",]
  BranchID value
2     NULL     2

Be carefull to not use a variable with the same name than the columns of your dataframe.

Clemsang
  • 5,053
  • 3
  • 23
  • 41
  • Just to give an alternative viewpoint: Besides all the added risks of using `attach` we could try to see the explicit mentioning of the data.frame name as an investment in the future: While it *does* take extra time to write the df name each time, it makes reading (and understanding) the code in all but the simplest cases much, much easier! And in most cases I spend more time looking at the code than writing it. For me: readability is more important than 'write-ability' (and they often go hand in hand), but your mileage my vary ;) – dario Mar 12 '20 at 12:07
  • Discussion of downsides and risks with `attach` e.g. [r-bloggers](https://www.r-bloggers.com/to-attach-or-not-attach-that-is-the-question/) and [so question](https://stackoverflow.com/questions/10067680/why-is-it-not-advisable-to-use-attach-in-r-and-what-should-i-use-instead) – dario Mar 12 '20 at 12:07