1

From the following dataset, I want a count of patient IDs with more than one visit.

pt_id <- c(1,1,1,1,1,2,2,2,3,3,3,3,3,4) visit <- c(2019, 2019, 2019, 2019, 2020, 2019, 2019, 2020, 2019, 2019, 2019, 2019, 2020, 2019) mydata <- data.frame(pt_id, visit) mydata 

4 Answers 4

1

additional option base R

sum(table(mydata$pt_id) > 1) #> [1] 3 
Sign up to request clarification or add additional context in comments.

Comments

1

Another option with count

library(dplyr) mydata %>% count(pt_id) %>% filter(n > 1) %>% nrow #[1] 3 

Comments

0

With dplyr:

library(dplyr) mydata %>% group_by(pt_id) %>% filter(n() > 1) %>% pull(pt_id) %>% unique %>% length # [1] 3 

With base - keep only duplicated ids, count unique values:

length(unique(mydata[duplicated(mydata$pt_id), ][["pt_id"]])) # [1] 3 

Comments

0

You can try this,

library(dplyr) mydata%>%group_by(pt_id)%>%mutate(count=ifelse(cumsum(n())>1,1,0)) 

If count is 1 then the patient had more than one visits and so on.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.