Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to check whether the sum of two columns equals to another column in r?

I have one column named "casual" and another column named "registered". I need to check whether they sum up to values in another column called "cnt". I tried the if_else function to create a new variable that if the sum equals the other column then put in "true" and if not "false", but it didn’t work and showed me error messages as below. How to make the if_else function work or are there other ways to see whether the sum of the two columns equals to a third column? Thank you!

The code I tried:


dcbikeshare <- dcbikeshare %>%

  mutate(dcbikeshare, check_sum = if_else(casual + registered = cnt, "TRUE", "FALSE"))

One error message:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Error: unexpected '=' in:
"dcbikeshare <- dcbikeshare %>%
  mutate(dcbikeshare, check_sum = if_else(casual + registered ="


Another error message:

Error in `mutate()`:
! Problem while computing `check_sum = if_else("0", "T", "F")`.
Caused by error in `if_else()`:
! `condition` must be a logical vector, not a character vector.

>Solution :

should be casual + registered == cnt. use == not = for comparison.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading