gpt4 book ai didi

R Data.table 根据另一列划分列中的值

转载 作者:行者123 更新时间:2023-12-03 19:57:42 25 4
gpt4 key购买 nike

我有一个 data.table,它有 364 行和 3 列:

Date        Weekday     Weight
2012-01-01 Monday 100
2013-01-02 Tuesday 200
...

帮助 data.table 7 行 2 列:
Weekday   Coefficient
Monday 0.91
Tuesday 0.84
Wednesday 0.99
...

现在我想在主 data.table 中创建一个基于工作日的“权重/系数”的第 4 列。
Weight_divided <- main[, Weight * help[Weekday==main$Weekday]$Coefficient]

结果如下:
Date        Weekday     Weight   Weight_divided
2012-01-01 Monday 100 91
2013-01-02 Tuesday 200 168
2012-01-03 Wednesday 300 297
2012-01-04 Thursday 400 256
2012-01-05 Friday 500 399
2012-01-06 Saturday 600 410
2012-01-07 Sunday 700 680
2012-01-08 Monday 300 NA <--
2012-01-09 Tuesday 600 NA <--
...

我想问题是两个 data.tables 的长度不同。
有没有办法如何在主 data.table 操作中引用这适用于较短的 data.table?

最佳答案

使用 data.table

library(data.table)
setkey(main, Weekday)[help, Weight_Coef := Weight*Coefficient][order(Date)]
# Weekday Date Weight Weight_Coef
# 1: Monday 2012-01-01 59 53.69
# 2: Tuesday 2012-01-02 45 37.80
# 3: Wednesday 2012-01-03 141 139.59
# 4: Thursday 2012-01-04 104 97.76
# 5: Friday 2012-01-05 133 109.06
#---
#360: Wednesday 2012-12-25 192 190.08
#361: Thursday 2012-12-26 79 74.26
#362: Friday 2012-12-27 39 31.98
#363: Saturday 2012-12-28 175 148.75
#364: Sunday 2012-12-29 134 116.58

数据
set.seed(24)
main <- data.table(Weekday=rep(c('Monday', 'Tuesday', 'Wednesday',
'Thursday', 'Friday', 'Saturday', 'Sunday'), length.out=364),
Date=seq(as.Date('2012-01-01'), length.out=364, by='day'),
Weight=sample(200, 364, replace=TRUE))

help <- data.table(Weekday=c('Monday', 'Tuesday', 'Wednesday',
'Thursday', 'Friday', 'Saturday', 'Sunday'), Coefficient=c(0.91, 0.84,
0.99, 0.94, 0.82, 0.85, 0.87))

关于R Data.table 根据另一列划分列中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30712493/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com