From a pragmatic point of view, it really does not matter all that much, they all get the job done in the same way which you are using them. (although there are ways in which these might fail if used in a function or loop, but in a script as they are, they seem equal)
From a computational standpoint, they are slightly more or less efficient, which when data becomes big, becomes meaningful.
You can test this.
Because 10 rows is computationally insignificant, I extended your data.frame out a bit, as follows:
df<- cbind(a=rnorm(1000000), b= rnorm(1000000))
RawData<-data.frame(df)
Running each with system.time
, you get the following:
system.time(RawData$c1 <- RawData$a + RawData$b , gcFirst = TRUE)
user system elapsed
0.008 0.001 0.009
system.time(RawData <- transform( RawData, c2 = a + b ),gcFirst = TRUE)
user system elapsed
0.008 0.001 0.009
system.time(RawData <- within( RawData, { c3 = a + b } ),gcFirst = TRUE)
user system elapsed
0.010 0.005 0.014
system.time(RawData$c4 <- with( RawData, a + b ), gcFirst = TRUE)
user system elapsed
0.006 0.004 0.010
Then I added another TWO zeros.
df<- cbind(a=rnorm(100000000), b= rnorm(100000000))
RawData<-data.frame(df)
Then reran the computations: AND WAITED A VERY LONG TIME...a very, very long time..I sent this series of tasks to work on a very fast machine before any answers were posted here this morning. Look at the elapsed time, the system time and the user time.
Clearly different methods have computational consequences when data gets large, and we are looking at simple tasks.
#The fastest method
system.time(RawData$c1 <- RawData$a + RawData$b , gcFirst = TRUE)
user system elapsed
5.542 244.188 3271.741
# The slowest method
system.time(RawData <- within( RawData, { c3 = a + b } ),gcFirst = TRUE)
user system elapsed
9.031 207.036 3794.536
These times are with all other applications closed, a clear environment and garbage collection between events!
Clearly how matters. The question becomes at what point do you worry about this sort of efficiency? The addition of two zeros takes the computation from fractional seconds to 54 and 63 minutes in elapsed time for each simple addition. Imagine if the math were more complex?
I would suspect if you took 42's advice using []
you could, improve performance even more....