Modi
Modi

Reputation: 143

R - Count / data manipulation across data table columns

I have below three columns in a data table : Delaware

Year and County are categorical columns.

+----+------+--------+
| No | Year | County |
+----+------+--------+
|  1 | 2011 | CHAVES |
|  2 | 2010 | CHAVES |
|  3 | 2010 | CHAVES |
|  4 | 2010 | CHAVES |
|  5 | 2010 | CHAVES |
|  6 | 2010 | CHAVES |
|  7 | 2010 | CHAVES |
|  8 | 2008 | CHAVES |
|  9 | 2008 | CHAVES |
| 10 | 2010 | CHAVES |
| 11 | 2010 | CHAVES |
| 12 | 2011 | CHAVES |
| 13 | 2011 | CHAVES |
| 14 | 2010 | CHAVES |
| 15 | 2006 | EDDY   |
| 16 | 2007 | EDDY   |
| 17 | 2008 | EDDY   |
| 18 | 2011 | EDDY   |
| 19 | 2013 | EDDY   |
| 20 | 2005 | EDDY   |
| 21 | 2010 | EDDY   |
| 22 | 2001 | EDDY   |
| 23 | 2001 | EDDY   |
| 24 | 2010 | EDDY   |
| 25 | 2009 | EDDY   |
+----+------+--------+

I can get the tabular results for each categorical column by using "table" as:

> table(Delaware$Year)

2001 2005 2006 2007 2008 2009 2010 2011 2013 
   2    1    1    1    3    1   11    4    1 
> table(Delaware$County)

CHAVES   EDDY 
    14     11 

But I would like to get the information as:

Give me count for each "county"; Chaves or Eddy as:

how many in 2006

how many in 2007

how many in 2008

how many in 2009..

Data in dput format.

See What can R do about a messy data format? on the posted data format.

Delaware <-
structure(list(No = 1:25, Year = structure(c(8L, 7L, 
7L, 7L, 7L, 7L, 7L, 5L, 5L, 7L, 7L, 8L, 8L, 7L, 3L, 
4L, 5L, 8L, 9L, 2L, 7L, 1L, 1L, 7L, 6L), 
.Label = c("2001", "2005", "2006", "2007", 
"2008", "2009", "2010", "2011", "2013"), 
class = "factor"), County = structure(c(1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = 
c("CHAVES", "EDDY"), class = "factor")), 
row.names = c(NA, -25L), class = "data.frame")

Upvotes: 0

Views: 30

Answers (1)

arg0naut91
arg0naut91

Reputation: 14774

Why not:

table(Delaware$Year, Delaware$County)

Upvotes: 2

Related Questions