2

I have a set of data :

Color Type axe

Green  1    2

Green  1    3

Green  0    1

Black  1    1

Black  0    2

Black  0    3

I want to return a table that tells me how many time a 'Type' is green or black and a 'axe' is green or black.

Type 

1   Green : 2 Black : 1

0   Green : 1 Black : 2

Axe

1   Green : 1 Black : 1

2   Green : 1 Black : 1

3   Green : 1 Black : 1

So a conditional count. I wanted to use the function 'table', but it just does a column occurrence count.

Thanks !

Simon O'Hanlon
  • 58,647
  • 14
  • 142
  • 184
LouInNY
  • 111
  • 1
  • 2
  • 9
  • Hi, welcome to SO. Since you are new here, you might find it helpful to read the [**about**](http://stackoverflow.com/about) and [**FAQ**](http://stackoverflow.com/faq) sections of the website to help you get the most out of it. If an answer does solve your problem you may want to *consider* upvoting and/or marking it as accepted to show the question has been answered, by ticking the little green check mark next to the suitable answer. You are **not** obliged to do this, but it helps keep the site clean of unanswered questions. – Simon O'Hanlon Aug 16 '13 at 16:48
  • Please also read [**how to make a great reproducible example**](http://stackoverflow.com/q/5963269/1478381) for how better to post a question next time. Welcome to SO! – Simon O'Hanlon Aug 16 '13 at 16:48
  • Related: http://stackoverflow.com/a/11562850/636656 – Ari B. Friedman Aug 16 '13 at 17:03

3 Answers3

3

How about table?

table( dat[ , c("Color" , "Type") ] )
       Type
Color   0 1
  Black 2 1
  Green 1 2

table( dat[ , c("Color" , "axe") ] )
       axe
Color   1 2 3
  Black 1 1 1
  Green 1 1 1
Simon O'Hanlon
  • 58,647
  • 14
  • 142
  • 184
2

Here is another alternative (also using table):

table(cbind(mydf[1], stack(mydf[-1])))
# , , ind = axe
# 
#        values
# Color   0 1 2 3
#   Black 0 1 1 1
#   Green 0 1 1 1
# 
# , , ind = Type
# 
#        values
# Color   0 1 2 3
#   Black 2 1 0 0
#   Green 1 2 0 0

Update

Here's a "reshape2" approach:

library(reshape2)
x <- melt(mydf)
# Using Color as id variables
y <- dcast(x, value + variable ~ Color)
# Aggregation function missing: defaulting to length
split(y[-2], y[[2]])
# $Type
#   value Black Green
# 1     0     2     1
# 2     1     1     2
# 
# $axe
#   value Black Green
# 3     1     1     1
# 4     2     1     1
# 5     3     1     1

Update 2

(To represent base R again)...

Building on @SimonO101's solution, here's a more automated way:

idvar <- "Color"
lapply(setdiff(names(mydf), idvar), function(y) table(mydf[, c(idvar, y)]))
# [[1]]
#        Type
# Color   0 1
#   Black 2 1
#   Green 1 2
# 
# [[2]]
#        axe
# Color   1 2 3
#   Black 1 1 1
#   Green 1 1 1
A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
  • I think this could be a bit misleading or easy to misinterpret because you do not have values `2` and `3` in the data in `type`, nor `0` in `axe`. – Simon O'Hanlon Aug 16 '13 at 16:21
0

You can use count from plyr package.

?count:

Equivalent to as.data.frame(table(x)), but does not include combinations with zero counts

library(plyr)
count(da,c('Color','Type'))

 Color Type freq
1 Black    0    2
2 Black    1    1
3 Green    0    1
4 Green    1    2
agstudy
  • 119,832
  • 17
  • 199
  • 261