1

I am currently trying to count the frequency of countries that appear in a dataframe object.

I tried using count commands as well as rle(sort(x)), which apparently is used to search for strings. But it does not seem to yield any results.

rle(sort(x))

I tried using this, but does not seem to work. I also tried to use

count(x, "COUNTRY") 

but all it does is count how many entries are there.

How can I get a result such as:

      Country        Frequency
[1]   United States      3
[2]   Mexico             5
[3]   Germany            12
camille
  • 16,432
  • 18
  • 38
  • 60
Mons
  • 13
  • 4

3 Answers3

1

Here is a small example using dplyr and the built-in dataset mtcars:

library(dplyr)

mtcars %>% 
  group_by(cyl) %>% 
  count(cyl)

or

mtcars %>% 
  group_by(cyl) %>% 
  add_count(cyl)
eastclintw00d
  • 2,250
  • 1
  • 9
  • 18
1

other solution is: table(yourdataframe$x)

Samuel Reuther
  • 101
  • 1
  • 3
1

count(x,Country,Frequency)

Have to include both to see a deeper breakdown then it'll count the countries and Frequency

or

X%>%group_by(Country)%>%summarise(sum = sum(Frequency), n = n())