R: counting frequency to use in levelplot
I have point series in a data.frame with duplication. I would like to plot them using level plot, and use as a Z frequency of x, y (in example how many times for x = 1 there was y = 2). How can I do this? Well it is easy for 开发者_JAVA技巧me to explain myself with SQL syntax:
SELECT x, y, count(*) from data_frame GROUP BY x, y
:)
These kinds of summarizing a data frame by groups defined by variables is very common in R. Many of the *pply functions would work. The standard response these days is to use ddply
from the plyr
package:
ddply(data_frame,.(x,y),summarise,total = NROW(piece))
In general, you should really learn the plyr
package, along with all the base *pply
functions.
But if you're more comfortable with SQL, you might consider looking at the sqldf
package, which lets you do a lot of these kinds of manipulations of data frames directly with SQL.
I don't doubt that many people get good results with plyr functions, but this is really a question that is easily answered with one of tapply
, table
, or ave
depending upon the unstated needs for this result. Both tapply
and table
would yield contingency tables which are really matrices or arrays and therefore very easy to access.
with( data_frame, table( x , y ) )
精彩评论