聚合R中的多个列上的表(),而不使用“by”分解 [英] Aggregating table() over multiple columns in R without a "by" breakdown
本文介绍了聚合R中的多个列上的表(),而不使用“by”分解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个2列x和y坐标点的数据框。我想生成一个表的每个点的出现次数。使用 table()
命令为所有可能的x-y对生成一个表。我可以用
I have a 2-column data frame of x- and y-coordinates of points. I want to generate a table of the number of occurrences of each point. Using the table()
command produces a table for all possible x-y pairs. I can eliminate the extras with
fullTable <- table(coords)
smalLTable <- subset(fullTable, fullTable > 0)
然后我相信我可以用 dimnames(fullTable)
来获得合适的坐标,但是有更好的方法吗?内置的东西?与
And then I'm sure I could do a little something with dimnames(fullTable)
to get the appropriate coordinates, but is there a better way? Something built in? Something that with
coords <- data.frame(x = c(1, 1, 2, 2, 3, 3), y = c(1, 1, 2, 1, 1, 1))
x y count
1 1 2
2 1 1
2 2 1
3 1 2
谢谢!
推荐答案
只使用Vanilla R,你可以做
Using just Vanilla R, you can do
aggregate(rep(1, nrow(coords)), by = list(x = coords$x, y = coords$y), sum)
这篇关于聚合R中的多个列上的表(),而不使用“by”分解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文