Ifelse statement within R's summarize function: dplyr

1.2k Views Asked by At

I'm trying to count the number of visits a provider has conducted if the visit meets a qualification in R. In the commented out phrase, I can get each provider with the correct number of total visits, but when I try to set an if statement, I'm getting the provider repeated multiple times and not the correct visit count.

TeleHealth_Counts %>%
  group_by(TeleHealth_Counts$`Visit Provider`) %>%
  summarize(Video_Count = ifelse(`Type` ==  "Video Visit New", NA, sum(`Visit Count`, na.rm = TRUE)))
  #summarize(Tele_Count = sum(`Visit Count`, na.rm = TRUE))

The other issue I'm facing is that when I assign this code to a variable so I can download the data, I'm getting an error: summarise() regrouping output by 'TeleHealth_Counts$Visit Provider' (override with .groups argument). How do I overcome this error or download the data frame I'm seeing in my console?

I've tried assigning it to a variable, to Tele_Count and to the data frame df_phys with the code below.

physicians <- unique(TeleHealth_Counts$`Visit Provider`)
df_phys <-data.frame(physicians)
1

There are 1 best solutions below

3
On BEST ANSWER

Type == "Video Visit New" creates a vector of length same as number of rows in the group and ifelse returns the output same length as the condition we are checking, hence it repeats the rows.

Try the following :

library(dplyr)

result <- TeleHealth_Counts %>%
  group_by(`Visit Provider`) %>%
  summarize(Video_Count = if(any(`Type` ==  "Video Visit New")) NA_real_ 
                          else sum(`Visit Count`, na.rm = TRUE))

The message that you are receiving is a warning and not an error which is safe to ignore since it's a default behavior in dplyr 1.0.0 onwards unless you silence the warning. To create a csv file of the above dataframe you can use write.csv like :

write.csv(result, 'result.csv', row.names = FALSE)