

23 September 2016




.Renviron fileupdate.package()update.packages(oldPkgs = "sp")
x = x + 1
involves a single function call to the + function. Whereas the for loop
for(i in seq_len(n)) x[i] = x[i] + 1
# Poorly indented/formatted code
if(!exists("x")){
x=c(3,5)
y=x[2]}
# Automatically indented code (Ctrl+I in RStudio)
if(!exists("x")){
x=c(3,5)
y=x[2]}
# Automatically reformat the code (Ctrl+Shift+A in RStudio)
if(!exists("x")) {
x = c(3, 5)
y = x[2]
}
Ctl-Up autocompletes last string beginning with current stringx = 1:100 # initiate vector to cumulatively sum
cs_for = function(x){
for(i in x){
if(i == 1){
xc = x[i]
} else {
xc = c(xc, sum(x[1:i]))
}
}
xc
}
cs_apply = function(x){
sapply(x, function(x) sum(1:x))
}
library(microbenchmark)
microbenchmark(cs_for(x), cs_apply(x), cumsum(x), times = 2)
## Unit: nanoseconds ## expr min lq mean median uq max neval ## cs_for(x) 186050 186050 228431 228431 270812 270812 2 ## cs_apply(x) 124669 124669 138334 138334 151999 151999 2 ## cumsum(x) 482 482 1281 1281 2080 2080 2