1

I'm often writing things like:

dataframe$this_column <- as.Date(dataframe$this_column)

That is, when changing some column in my data frame [table], I'm constantly writing the column twice. Is there some function that allows me to directly change the data frame w/o explicitly reassigning it? Say: ch(dataframe$this_column, as.Date())

EDIT: While similar, the potential duplicate is not the same. I am not looking for a way to shorten self-referential reassignments. I'm looking to avoid the explicit reassignment all together. The answer I accepted here is an appropriate solution (and much better than the answers provided in the "duplicate" question, in regards to their relevance to my question).

AmagicalFishy
  • 1,249
  • 1
  • 12
  • 36
  • 4
    `%<>%` in `magrittr` package? – Kota Mori Mar 25 '16 at 17:32
  • What do you mean precisely with "writing the column twice"? – Worice Mar 25 '16 at 17:35
  • @KotaMori, post as answer? – Ben Bolker Mar 25 '16 at 17:37
  • Nearly every programming language that I've ever used has done it this way. What is the advantage to your new way? – cory Mar 25 '16 at 17:42
  • @cory Uh... less typing. That's pretty clearly the problem here. – AmagicalFishy Mar 25 '16 at 17:46
  • @AmagicalFishy Is it really clear? It wasn't clear to me that the number of characters was the problem, especially because there are the same number of characters (25) in your proposed solution as there is in the base R way. – cory Mar 25 '16 at 17:48
  • @cory Ok, I changed my question to make it easier for you to possibly *fathom* the question. :l Why else would I talk about not wanting to type the same column twice? – AmagicalFishy Mar 25 '16 at 17:55
  • Possible duplicate of [R self reference](http://stackoverflow.com/questions/7768686/r-self-reference) – cory Mar 25 '16 at 18:15
  • Regarding your edit: the solution that was proposed does NOT avoid the reassingment, it just shorter syntax, but still explicitly reassigns. You cannot avoid the reassignment, because an R function does not modify it's inputs. – Stibu Mar 26 '16 at 08:57
  • @Stibu This is just a semantic difference. The solution works because the performed reassignment is *implicit* in the proposed operator (that is, it's not explicitly done by *me*). Ultimately, any change in a variable will be a reassignment. – AmagicalFishy Mar 28 '16 at 14:39

1 Answers1

3

Here is the example using magrittr package:

library(magrittr)
x = c('2015-12-12','2015-12-13','2015-12-14')
df = data.frame(x)
df$x %<>% as.Date 
cgage1
  • 579
  • 5
  • 15