简体   繁体   中英

Aggregating table() over multiple columns in R without a “by” breakdown

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 xy pairs. I can eliminate the extras with

fullTable <- table(coords)
smalLTable <- subset(fullTable, fullTable > 0)

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))

would return

x y count
1 1 2
2 1 1
2 2 1
3 1 2

仅使用Vanilla R,您就可以

aggregate(rep(1, nrow(coords)), by = list(x = coords$x, y = coords$y), sum)

Better than ddply is count :

library(plyr)
count(coords)

It's a lot faster than table for sparse 2d results too.

You could also use data.table

library(data.table)
DT <- data.table(coords)
DT[,.N,by=list(x,y)]
##   x y N
## 1: 1 1 2
## 2: 2 2 1
## 3: 2 1 1
## 4: 3 1 2

See this answer for more details on the use of .N and creating frequency tables with data.table

您可以从plyr库中使用ddply

plyr::ddply(coords, .(x, y), summarize, count = length(x))

With dplyr

library(dplyr)
count(coords, x, y)

With data.table

library(data.table)
setDT(coords)
coords[, .(n = .N), by = .(x, y)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM