Why do these two methods give me different results?

# Find invalid totals
accounts %>%
  # theoretical_total: sum of the three funds
  mutate(theoretical_total=fund_A+fund_B+fund_C) %>%
  # Find accounts where total doesn't match theoretical_total
  filter(theoretical_total != total)

I just replace + with sum, but I didn't get the right result. Why?

# Find invalid totals
accounts %>%
  # theoretical_total: sum of the three funds
  mutate(theoretical_total=sum(fund_A, fund_B, fund_C)) %>%
  # Find accounts where total doesn't match theoretical_total
  filter(theoretical_total != total)

its because the second requires rowwise() to make the summation at the row level rather than table level

accounts %>%
   rowwise() %>% 
mutate( ... etc.)
1 Like

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