在R中的多个列上聚合表(),但不带“;由「;崩溃
我有一个两列的数据框,包括点的x坐标和y坐标。我想生成一个表,列出每个点的出现次数。使用在R中的多个列上聚合表(),但不带“;由「;崩溃,r,dataframe,aggregate,R,Dataframe,Aggregate,我有一个两列的数据框,包括点的x坐标和y坐标。我想生成一个表,列出每个点的出现次数。使用table()命令为所有可能的x-y对生成一个表。我可以用它来消除多余的东西 fullTable <- table(coords) smalLTable <- subset(fullTable, fullTable > 0) 会回来吗 x y count 1 1 2 2 1 1 2 2 1 3 1 2 只要使用香草R,你就可以 aggregate(rep(1, nrow(coords))
table()
命令为所有可能的x-y对生成一个表。我可以用它来消除多余的东西
fullTable <- table(coords)
smalLTable <- subset(fullTable, fullTable > 0)
会回来吗
x y count
1 1 2
2 1 1
2 2 1
3 1 2
只要使用香草R,你就可以
aggregate(rep(1, nrow(coords)), by = list(x = coords$x, y = coords$y), sum)
您可以使用
plyr
库中的ddply
plyr::ddply(coords, .(x, y), summarize, count = length(x))
比ddply更好的是
count
:
library(plyr)
count(coords)
对于稀疏2d结果,它也比table快得多。您也可以使用
数据。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
library(data.table)
setDT(coords)
coords[, .(n = .N), by = .(x, y)]
库(data.table)
DT与dplyr
library(dplyr)
count(coords, x, y)
带有数据。表
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
library(data.table)
setDT(coords)
coords[, .(n = .N), by = .(x, y)]
正是我想要的。谢谢coords
是一个data.frame(已经是一个列表),因此稍微短一点的解决方案是:aggregate(coords$x,by=coords,length)
。