1

What I want to do is check a data frame of ordered pairs of x and y values (in this particular set there are only 4 possible values) and count how many times each particular value happens.

For example, my data frame has columns 1 and 2 as follows:

data <- data.frame(col1=c(-.25, 0, -.25, -.77, 0, 0, 0, -2.5),
                   col2=c(0.9, 0, 0.9, 2.9, 0, 0, 0, 0.9))

And I would like to count the times each possible pair arises. The closest thing I have found is table() which returns 16 possible values, which is not what I need. I.E, I try

test=as.data.frame(table(data[,1:2]))

which feeds in the two columns into a table as test. What I get back is a matrix enumerating 9 'possible' combinations - I guess what it does is simply cross-reference each column value and count the number of times it arises.

Edit: I realize that the non-zero entries in the third column of the table are the ones I want, but I eventually need this to perform things with 24 total sets, so it should be automated as possible.

Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
yawgeh
  • 97
  • 1
  • 10
  • Can you give a small reproducible example and what the expected result would look like? Use code folding if possible. – Roman Luštrik May 27 '13 at 20:15
  • I'm not sure how `table(data)` doesn't do this. It *does* provide a count of each possible pairing in the two columns. Do you just need to remove the zero values? – Noam Ross May 27 '13 at 20:21
  • Yes, I essentially just need to remove the zero values. I just played around further and noticed that. – yawgeh May 27 '13 at 20:22

2 Answers2

3

Given the high number of sets (24), table might create too many combinations. You could use this alternative approach:

data$count <- 1
aggregate(count ~ ., data, FUN = sum)
#    col1 col2 count
# 1  0.00  0.0     4
# 2 -2.50  0.9     1
# 3 -0.25  0.9     2
# 4 -0.77  2.9     1
flodel
  • 87,577
  • 21
  • 185
  • 223
0

Subset test to remove zero values:

test=as.data.frame(table(data[,1:2]))
test <- test[which(!test$Freq==0),]

##    col1 col2 Freq
##4      0    0    4
##5   -2.5  0.9    1
##7  -0.25  0.9    2
##10 -0.77  2.9    1
Noam Ross
  • 5,969
  • 5
  • 24
  • 40