-1
我想在現有數據框中用一個參數(worker_id)計算一個新的行(difference_mean_by_worker_id)和平均值(差異)。每一個worker_id的差異的平均值應該在新行中相同。 像這樣:在數據框中用一個參數計算一個新行
謝謝,蒂姆
我想在現有數據框中用一個參數(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
分區組中的距離。希望這可以幫助!
感謝您的意見和你的幫助:) –
這是一個非常基本的任務。你應該在網上找到很多輸入。我建議你使用package data.table。或者建立tapply功能。 –
只是'ave(df $差異,df $ worker_id)'。不要張貼圖片;而是複製/粘貼您的數據集(或其中的一部分),以便每個人都可以使用它。 – nicola