Q:

R: Applying normalization function column wise - large DataFrame/DataTable

I have a large r data.frame with close to 500 columns. I want to add existing scale function and also try out different normalization function in a column wise fashion. 
As of existing scale function 
library(dplyr)

set.seed(1234)
dat <- data.frame(x = rnorm(10, 30, .2), 
                  y = runif(10, 3, 5),
                  z = runif(10, 10, 20), k = runif(10, 5, 10))

dat %>% mutate_each_(funs(scale),vars=c("y","z")) 

Question1:
In this case vars are only two but when you have 500 columns to normalized whats the best way?
I tried following: 
dnot <- c("y", "z")
dat %>% mutate_each_(funs(scale),vars=!(names(dat) %in% dnot)) 

Error: 
Error in UseMethod("as.lazy_dots") : 
  no applicable method for 'as.lazy_dots' applied to an object of class "logical"

Question2: Instead of using inbuilt scale function I want to apply my own function to normalize the data frame. 
example: I have following function 
normalized_columns <- function(x)
{
  r <- (x/sum(x))
}

Question2: How can I efficiently apply this to all the columns while leaving out only 3 or 4 columns. 

A:

As the OP used dplyr methods, one option would be using setdiff with mutate_each_
dat %>%
     mutate_each_(funs(scale), setdiff(names(dat), dnot)) 
#               x        y        z           k
#1  -0.8273937 3.633225 14.56091  0.22934964
#2   0.6633811 3.605387 12.65187  0.76742806
#3   1.4738069 3.318092 13.04672 -1.16688369
#4  -1.9708424 3.079992 15.07307  0.62528427
#5   0.8157183 3.437599 11.81096 -1.06313355
#6   0.8929749 4.621197 17.59671 -0.06743894
#7  -0.1923930 4.051395 12.01248  0.94484655
#8  -0.1641660 4.829316 12.58810 -0.16575678
#9  -0.1820615 4.662690 19.92150 -1.55940662
#10 -0.5090247 3.091541 18.07352  1.45571106

Or subset the names based on the logical index
dat %>%
    mutate_each_(funs(scale), names(dat)[!names(dat) %in% dnot])
#           x        y        z           k
#1  -0.8273937 3.633225 14.56091  0.22934964
#2   0.6633811 3.605387 12.65187  0.76742806
#3   1.4738069 3.318092 13.04672 -1.16688369
#4  -1.9708424 3.079992 15.07307  0.62528427
#5   0.8157183 3.437599 11.81096 -1.06313355
#6   0.8929749 4.621197 17.59671 -0.06743894
#7  -0.1923930 4.051395 12.01248  0.94484655
#8  -0.1641660 4.829316 12.58810 -0.16575678
#9  -0.1820615 4.662690 19.92150 -1.55940662
#10 -0.5090247 3.091541 18.07352  1.45571106

If we are using mutate_each, another option is one_of
dat %>%
     mutate_each(funs(scale), -one_of(dnot))   
#         x        y        z           k
#1  -0.8273937 3.633225 14.56091  0.22934964
#2   0.6633811 3.605387 12.65187  0.76742806
#3   1.4738069 3.318092 13.04672 -1.16688369
#4  -1.9708424 3.079992 15.07307  0.62528427
#5   0.8157183 3.437599 11.81096 -1.06313355
#6   0.8929749 4.621197 17.59671 -0.06743894
#7  -0.1923930 4.051395 12.01248  0.94484655
#8  -0.1641660 4.829316 12.58810 -0.16575678
#9  -0.1820615 4.662690 19.92150 -1.55940662
#10 -0.5090247 3.091541 18.07352  1.45571106

The setdiff option with data.table would be
library(data.table)
nm1 <- setdiff(names(dat), dnot)
setDT(dat)[, (nm1) := lapply(.SD, scale), .SDcols = nm1]

A:

There are better approaches, but I usually do something like:
set.seed(1234)
x = rnorm(10, 30, .2)
y = runif(10, 3, 5)
z = runif(10, 10, 20)
k = runif(10, 5, 10)
a = rnorm(10, 30, .2)
b = runif(10, 3, 5)
c = runif(10, 10, 20)
d = runif(10, 5, 10)

normalized_columns <- function(x)
{
x/sum(x)
}

dat<-data.frame(x,y,z,k,a,b,c,d)
dat[,c(1,4,6:8)]<-sapply(dat[,c(1,4,6:8)], normalized_columns)

Edit: as far as efficiency goes, this is pretty fast:
set.seed(100)
dat<-data.frame(matrix(rnorm(50000, 5, 2), nrow = 100, ncol = 500))
cols<-sample.int(500, 495, replace = F)
system.time(dat[,cols]<-sapply(dat[,cols], normalized_columns))
##user  system elapsed 
##0.03    0.00    0.03

