An incredibly common operation for my type of data is applying a normalisation factor to all columns. This can be done efficiently using sweep
or scale
:
normalized = scale(data, center = FALSE, scale = factors)
# or
normalized = sweep(data, 2, factors, `/`)
Where
data = structure(list(A = c(3L, 174L, 6L, 1377L, 537L, 173L),
B = c(1L, 128L, 2L, 1019L, 424L, 139L),
C = c(3L, 66L, 2L, 250L, 129L, 40L),
D = c(4L, 57L, 4L, 251L, 124L, 38L)),
.Names = c("A", "B", "C", "D"),
class = c("tbl_df", "data.frame"), row.names = c(NA, -6L))
factors = c(A = 1, B = 1.2, C = 0.8, D = 0.75)
However, how do I do this with dplyr, when my data has additional columns in front? I can do it in separate statements, but I’d like doing it in one pipeline. This is my data:
data = structure(list(ID = c(1, 2, 3, 4, 5, 6),
Type = c("X", "X", "X", "Y", "Y", "Y"),
A = c(3L, 174L, 6L, 1377L, 537L, 173L),
B = c(1L, 128L, 2L, 1019L, 424L, 139L),
C = c(3L, 66L, 2L, 250L, 129L, 40L),
D = c(4L, 57L, 4L, 251L, 124L, 38L)),
.Names = c("ID", "Type", "A", "B", "C", "D"),
class = c("tbl_df", "data.frame"), row.names = c(NA, -6L))
And I’d like to mutate the data columns without touching the first two columns. Normally I can do this with mutate_each
; however, how I cannot pass my normalisation factors to that function:
data %>% mutate_each(funs(. / factors), A:D)
This, unsurprisingly, assumes that I want to divide each column by factors
, rather than each column by its matching factor.
See Question&Answers more detail:
os