【发布时间】:2018-08-31 13:36:08
【问题描述】:
我的数据集具有以下结构:
id amount zipcode cat1 cat1_times cat2 cat2_times
1 1000 1001 0 0 1 7
2 2000 1001 0 0 1 7
3 2300 1002 1 6 1 5
4 1500 1002 1 6 1 5
5 2700 1003 1 3 1 5
6 3400 1003 1 3 1 5
Cat1 是一个二进制变量,如果在某个邮政编码中存在类别 1 的建筑物,则取值为 1。 Cat1_times 是某个邮政编码中类别 1 的建筑物数量。 我想计算每一行的建筑总数(cat1 + cat2):
id amount zipcode cat1 cat1_times cat2 cat2_times total_times
1 1000 1001 0 0 1 7 7
2 2000 1001 0 0 1 7 7
3 2300 1002 1 6 1 5 11
4 1500 1002 1 6 1 5 11
5 2700 1003 1 3 1 5 8
6 3400 1003 1 3 1 5 8
我尝试了 sum(cat1_times,cat2_times),但每一行的结果都相同。
【问题讨论】:
-
df$total_times = df$cat1_times + df$cat2_times应该可以工作。 -
那么简单
df$cat1_times + df$cat2_times??... -
谢谢大家,这两种方法都可以,你知道为什么函数sum()不工作吗?