0

I am trying to add a column of flagged data, where "Secret Identity is 0, and everything else is 1. My table currently looks like this;

name ID

Spider-Man Secret Identity

Captain America Public Identity

Wolverine Public Identity

Iron Man Public Identity

Thor No Dual Identity

Benjamin Grimm Public Identity

Reed Richards Public Identity

Otto Octavius Secret Identity

Hulk Public Identity

Scott Summers Public Identity

Johnathan Storm Public Identity

Katherine Pryde Secret Identity

As for coding it;

characters %>% mutate(FLAG = case_when(ID == "Secret Identity" ~ 0, TRUE ~ 1))

This seems to work, however I also needed a new field or column listing either '0' or '1' depending on the ID field. Like this;

name ID Flag

Spider-Man Secret Identity 1

Captain America Public Identity 0

Wolverine Public Identity 0

Iron Man Public Identity 0

Thor No Dual Identity 0

Benjamin Grimm Public Identity 0

Reed Richards Public Identity 0

Otto Octavius Secret Identity 1

Hulk Public Identity 0

Scott Summers Public Identity 0

Johnathan Storm Public Identity 0

Katherine Pryde Secret Identity 1

  • In general, it's a better idea to use `logical` class instead of integers 1 and 0 to represent "yes"/"no" binary data. https://sscc.wisc.edu/sscc/pubs/dwr/logical.html – shadowtalker Feb 21 '23 at 01:32
  • As for your problem, don't forget that you can use regular programming constructs like `for` loops to construct several columns automatically. This is a case where Dplyr gets a [little clunky](https://stackoverflow.com/q/26003574/2954547), and base R works a bit nicer, because in base R you can just operate on the column names as regular text data. – shadowtalker Feb 21 '23 at 01:34
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Feb 21 '23 at 13:19

0 Answers0