1

I have something like this:

# A tibble: 24,288 x 1

Country/Region

Afghanistan
Albania
Algeria
Andorra
Angola
Antigua and Barbuda Argentina
Armenia
Australia
Australia

... with 24,278 more rows

How can I count the different values in this tibble?

1
  • Do you need n_distinct(df$`Country/Region`) or length(unique(df$`Country/Region`)) or uniqueN(df$`Country/Region`) ? All of them count distinct values in the column.
    – Ronak Shah
    Apr 24, 2020 at 3:25

2 Answers 2

1

We can use count from dplyr

library(dplyr)
df1 %>%
   count(`Country/Region`)
4
  • there are two ore more rows with same values. I want to do a distinct count. How can I do that?
    – Leko
    Apr 23, 2020 at 23:22
  • @Leko The count will gives the distinct frequency count. Sorry, didn't understand your question
    – akrun
    Apr 23, 2020 at 23:23
  • 1
    It was something wrong with my code so the count function wasn't working. it works with tydiverse package: select () %>% distinct() thank u for help.
    – Leko
    Apr 24, 2020 at 23:01
  • 1
    @Leko it is possible that count is masked from some other package count function. In that case, you can speciify the package with dplyr::count(Country/Region)
    – akrun
    Apr 25, 2020 at 18:37
1

Here's the sqldf solution:

textFile <- "Country_Region
Afghanistan
Albania
Algeria
Andorra
Angola
Antigua and Barbuda Argentina
Armenia
Australia
Australia"

data <- read.csv(text = textFile,stringsAsFactors = FALSE)
library(sqldf)
sqldf("select count(distinct Country_Region) from data")

...and the result:

> sqldf("select count(distinct Country_Region) from data")
  count(distinct Country_Region)
1                              8
> 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.