0

I have a data set like the one below. I would like to make TABLE A into TABLE B. i.e. How can I make a new dataframe where Birthplace is my 1st coloumn, and frequency is the second (seen in second table below).

TABLE A

Person     Birthplace
 Nick        Berlin
 Alex        Berlin
 Mark        Toronto
 Jane        New York
 Bob         Toronto
 Ruth        New York
 Steven      Berlin

TABLE B

Birthplace     Frequency
 Berlin            3
 Toronto           2
 New York          2






NickA
  • 433
  • 2
  • 10

2 Answers2

1

with dplyr:

you can use the following:

tableA %>% group_by(Birthplace) %>% summarise(Frequency = n())
MKR
  • 1,620
  • 7
  • 20
1

Using dplyr count:

> df
# A tibble: 7 x 2
  Person Birthplace
  <chr>  <chr>     
1 Nick   Berlin    
2 Alex   Berlin    
3 Mark   Toronto   
4 Jane   New York  
5 Bob    Toronto   
6 Ruth   New York  
7 Steven Berlin    
> 

> df %>% count(Birthplace, name = 'Frequency')
# A tibble: 3 x 2
  Birthplace Frequency
  <chr>          <int>
1 Berlin             3
2 New York           2
3 Toronto            2
Karthik S
  • 11,348
  • 2
  • 11
  • 25