I want to calculate count of unique SUBJID according to severity so that it counts SUBJID with higher grade severity

I want to calculate count of unique SUBJID according to severity so that it counts SUBJID only with higher grade severity using group by function of "dplyr" package. so that it will give out of grade 1 is 11 not 16 because grade 2 and grade 3 SUBJID are overlapping with grade 1.

i'm attaching images for the reference.

adae <- read.csv("C:/Studies/Rajat/X4/ADaM/ADaM/adae.csv")
head(adae)

ae=select(adae, SAFFL,TRTEMFL,SUBJID, AETOXGR,ASEV,AREL,RELCOMB,RELSTYPR);ae
ae1=filter(ae,SAFFL=="Y" );ae1

a1=ae1 %>% group_by(AETOXGR) %>%

summarise(N=length(unique(SUBJID)), E=length(SUBJID))

View(a1)

required output :
N E
grade 1 11 34
grade 2 4 6
grade 3 1 1

Please help with this topic.

here is an example of how to approach

library(tidyverse)
(exmpl_df <- tibble(
  subject_id = c(1,1:5,5, 1, 6),
  grp = c(1,rep(1, 5),5, 2, 2)
))


(exmpl_df2 <- group_by(
  exmpl_df,
  subject_id
) |> summarise(max_group = max(grp, na.rm = TRUE)))

(exmpl_df3 <- exmpl_df |> 
    left_join(exmpl_df2) |> 
    select(-grp))

# full results
exmpl_df3 |> split(~max_group)

# results simplified to give answer - number of unique subject_id by group
exmpl_df3 |> split(~max_group) |> lapply(\(x)length(unique(x$subject_id)))

I need all variables of data because keep.all is not working inside the summarise function.

required output :
N E
grade 1 11 34
grade 2 4 6
grade 3 1 1

where N is the count of unique SUBJID with priority according to grades and E is the total SUBJID count without prioritize gradewise

adapt my code example to your own data....
Because of the join; it inherently would maintain your other columns; select is used to have more or less columns.
Your screenshot is not useful to me because I can't access it as a data.frame in R .

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.