r count distinct dplyr

To count distinct values in a column using the dplyr package in R, you can follow these steps:

  1. Load the dplyr package:
library(dplyr)
  1. Create a data frame or use an existing data frame:
data <- data.frame(column_name = c("A", "B", "A", "C", "B", "D"))
  1. Use the distinct() function from dplyr to get unique values in the specified column:
distinct_data <- distinct(data, column_name)
  1. Use the nrow() function to count the number of rows in the new data frame:
count <- nrow(distinct_data)
  1. Print the count to see the result:
print(count)

These steps will help you count the distinct values in a specific column using the dplyr package in R.

I hope this explanation helps! Let me know if you have any further questions.