我想通过现有数据框中的一个参数(worker_id)计算一个新行(difference_mean_by_worker_id)的平均值(差异)。新行中每个 worker_id 的差异均值应该相同。像这样:
谢谢,蒂姆
这是一个data.table
解决方案:
library(data.table);
# make the data
df = data.table(
worker_id = c(111, 111, 222, 222),
difference = c(5, 3, 5, 2)
);
# calculate mean difference
df_new = df[
,
# make a new column called "difference_mean_by_worker_id" to be the mean of
# "difference"
"difference_mean_by_worked_id" := mean(x = difference),
# grouped by worker_id
by = "worker_id"
];
df_new;
worker_id difference difference_mean_by_worked_id
1: 111 5 4.0
2: 111 3 4.0
3: 222 5 3.5
4: 222 2 3.5
此脚本计算由 划分的组中距离的平均值worker_id
。希望这可以帮助!