Total TRUE and FALSE in Grouped Data

I am having trouble calculating the number of TRUE and FALSE values based off specific groups. I think this is pretty basic, but essentially I have data with many IDs and IDs that have a value of either TRUE or FALSE in a column titled one_positive (this was created with mutate() and any() to assign a TRUE value if just one of several tests was listed as positive).

Now, I am trying to calculate the total number of TRUE and FALSE one_positives per unique ID in a given a month.

My end goal is a dataframe that has three columns: Lab Month, # True, # False

chla_gono %>%
  mutate(positive = ifelse(labval %in% c("DETECTED", "POS", "POSITIVE", "REACTIVE", "REACTIVEMINIMAL", "REPEATEDLY REACTIVE", "RESULT:            NEISSERIA GONORRHOEAE"), "TRUE", "FALSE")) %>% 
  group_by(labdate,lab_month, iasid) %>% 
  mutate(one_positive= any(positive== "TRUE")) %>% 
  group_by(lab_month, iasid, one_positive) %>% 
  count(one_positive)

When doing this code, I get a column that lists the total number of tests that each unique ID has associated during a given time frame.

Is this what you want?

chla_gono %>%
  mutate(positive = ifelse(labval %in% c("DETECTED", "POS", "POSITIVE", "REACTIVE", "REACTIVEMINIMAL", "REPEATEDLY REACTIVE", "RESULT:            NEISSERIA GONORRHOEAE"), "TRUE", "FALSE")) %>% 
  group_by(lab_month) %>% 
  summarise(
    n_positive= sum(positive),
    n_negative= sum(!positive)
  )

No that doesn't give me what I'm looking for... I'm trying to count all the TRUE and FALSE values for each month by unique ID. But thanks for your response!

Then include the additional grouping variables in group_by(). But you said:

"My end goal is a dataframe that has three columns: Lab Month, # True, # False"

1 Like

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.