在R中的多个列上聚合table()而没有"by"细分

Gre*_*gor 5 aggregate r dataframe

我有一个2列数据框的x和y坐标点.我想生成一个表中每个点的出现次数.使用该table()命令可为所有可能的xy对生成一个表.我可以消除额外的东西

fullTable <- table(coords)
smalLTable <- subset(fullTable, fullTable > 0)
Run Code Online (Sandbox Code Playgroud)

然后我确信我可以用一些东西dimnames(fullTable)来获得适当的坐标,但是有更好的方法吗?内置的东西?与之相关的东西

coords <- data.frame(x = c(1, 1, 2, 2, 3, 3), y = c(1, 1, 2, 1, 1, 1))
Run Code Online (Sandbox Code Playgroud)

会回来的

x y count
1 1 2
2 1 1
2 2 1
3 1 2
Run Code Online (Sandbox Code Playgroud)

ada*_*ich 9

使用香草R,你可以做到

aggregate(rep(1, nrow(coords)), by = list(x = coords$x, y = coords$y), sum)
Run Code Online (Sandbox Code Playgroud)


had*_*ley 7

比ddply更好的是count:

library(plyr)
count(coords)
Run Code Online (Sandbox Code Playgroud)

对于稀疏的2d结果,它比表快很多.