简体   繁体   中英

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:

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM