-2

我想通过现有数据框中的一个参数(worker_id)计算一个新行(difference_mean_by_worker_id)的平均值(差异)。新行中每个 worker_id 的差异均值应该相同。像这样:

在此处输入图像描述

谢谢,蒂姆

4

1 回答 1

0

这是一个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。希望这可以帮助!

于 2017-09-27T09:00:32.530 回答