# Having trouble calculating something, am I missing a function ?

Hello guys, I'm having trouble by doing something.
Imagine I got 4 columns (col1, col2,col3,col4)
I'm bad at explanation so a screen will be easier : I would like a 5th column, who gives me, as an example here, 1000(col4) , split by every A (col2), and the split depend on the col3 (250,144,18) as proportion of 1000.
So col5 would be something like 600, 346,44 for the first three rows

forgot to add that col2 A will always correspond with col4 1000
every time there is an "A" at col2, col4 will always be "1000"
I'm having trouble searching on this forum because I don't know how to write my problem.
Thanks you !

``````(df1 <- data.frame(
col2 = c(rep("a", 3), rep("b", 3)),
col3 = c(250, 144, 18, 1225, 4200, 1777),
col4 = c(rep(1000, 3), rep(2000, 3))
))

library(tidyverse)

(df2 <- group_by(
df1,
col2
) %>% summarise(col3_sum =
sum(col3,
na.rm = TRUE)) %>%
ungroup())

(df3 <- left_join(df1,
df2,
by = "col2") %>%
mutate(col4x =
(col4 * col3) /
col3_sum))``````

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.