I have a dataframe:
df <- data.frame(x = 1:5, y = rep(1,5), z = 0:4,
fx = NA_real_, fy = NA_real_, fz = NA_real_)
my_count_columns <- c("x", "y", "z")
I want to fill in information by mutating in place columns fx
, fy
, fz
that represents the frequency of each count variable.
What is the cleanest way to do this in dplyr/tidyverse, assuming I don't know the column names ahead of time?
Expected output:
x y z fx fy fz
1 1 1 0 0.06666667 0.2 0.0
2 2 1 1 0.13333333 0.2 0.1
3 3 1 2 0.20000000 0.2 0.2
4 4 1 3 0.26666667 0.2 0.3
5 5 1 4 0.33333333 0.2 0.4