Dynamic mutate_each in dplyr

83 views Asked by At

I have the following columns in my dataframe:

c1_sum | c2_sum | d | c1 | c2

The columns c# and c#_sum are dynamic. I'm trying to do something like this for all c#:

mutate(c#_weight = (d * c#) / c#_sum)

The final result would be:

c1_sum | c2_sum | d | c1 | c2 | c1_weight | c2_weight

I already tried something with matches to handle only the column I want, but I don't know how to parse the current column name (c#_weight = (d * c#) / c#_sum).

mutate_each(funs(weight = .), matches("^c[0-9]*$", ignore.case=FALSE)) 

I cannot use tidyr because I'm using sparklyr!! Is there any way to do it using only dplyr??

0

There are 0 answers