【发布时间】:2019-04-30 06:20:05
【问题描述】:
我有一个数据表df,我想按第一列“位置”对其进行分组,
并计算所有表中条目的频率,包括列和行:
df
location NN_1 NN_2 NN_3
NYC 17 17 17
NYC 17 16 1
LA 1 1 10
LA 16 10 1
可以通过以下方式获得:
df <- structure(list(location = c("NYC", "NYC", "LA", "LA"),
NN_1 = c(17, 17, 1, 16),
NN_2 = c(17, 16, 1, 10),
NN_3 = c(17, 1, 10, 1)),
class = "data.frame",
row.names = c(NA, -4L))
我想计算17 在给定位置重复了多少次,例如NYC:
output
location NNs freq
NYC 17 4
NYC 16 1
NYC 1 1
LA 1 3
LA 16 1
LA 10 2
【问题讨论】: