I have a data.frame
with variables var1
var2
(both strings) and variables x
, y
, and z
. I would like to normalize variables x
, y
and z
by dividing them all by their respective first element.
I tried:
df_ %>%
mutate_at(c("x", "y", "z"), funs(./.[1])) %>% head()
But, this sets the whole column to 1. How can I achieve that it devides by the first element?
Secondly, what is the best way to add the normalized to the dataframe as variables x_norm
, y_norm
, z_norm
?
Many thanks, and please let me know in case you need further info.
It could be a problem with the attributes or grouping variable. We can reset the dataset without external attributes by converting to
data.frame
and then do themutate_at
Building on @akrun's answer, you can also use the
first()
function fromdplyr
: