展开数据框 [英] Expand Data Frame

查看:16
本文介绍了展开数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在某些条件下扩展数据框.它有点类似于这个问题expand data frames inside data frame,但是不太一样.

I want to expand a data frame given some conditions. It is a bit similar to this question expand data frames inside data frame, but not quite the same.

我有一个数据框:

df = data.frame(ID = c(3,3,3,3, 17,17,17, 74, 74, 210, 210, 210, 210), amount = c(101, 135, 101, 68,  196, 65 ,135, 76, 136, 15, 15, 15 ,15), week.number = c(4, 6, 8, 10, 2, 5, 7, 2, 6, 2, 3, 5, 6))

我想扩展每个 ID 的数据框,给定最小和最大周数,并且在此扩展的金额列中为 0.最小周数为 1,最大周数为 10.预期结果为:

I want to expand the data frame for each ID, given a min and max week.number, and having 0 in the amount column for this expansion. Min week.number is 1 and max week.number is 10. The expected results would be:

df1 <- data.frame(ID = c(rep(3,10), rep(17, 10), rep(74, 10), rep(210, 10)),
              amount = c(0, 0, 0, 101, 0, 135, 0, 101, 0, 68, 0, 196,
                         0, 0, 65, 0, 135, 0, 0, 0, 0, 76, 0, 0, 0,
                         136, 0, 0, 0, 0, 0, 15, 15, 0, 15, 15, 0, 0,
                         0, 0))

(实际上,我有数千个 ID,周数从 1 到 160).

(In reality, I have thousands of ID and week number goes from 1 to 160).

有没有简单快捷的方法来做到这一点?

Is there a simple, fast way to do this?

谢谢!

推荐答案

With data.table (tx to Frank 修正结果的长度):

With data.table (tx to Frank for correcting the length of the result):

require(data.table)
dt<-as.data.table(df)
f<-function(x,y,len=max(y)) {res<-numeric(len);res[y]<-x;res}
dt[,list(amount=f(amount,weeek.number,10)),by=ID]
#     ID amount
# 1:   3      0
# 2:   3      0
# 3:   3      0
# 4:   3    101
# 5:   3      0
# 6:   3    135
# 7:   3      0
# 8:   3    101
# 9:   3      0
#10:   3     68
# ......

编辑

我刚刚注意到你的 amountweeek.number 实际上定义了一个 sparseVector,即一个主要由零组成的向量,其中只有索引的非零元素被保留.因此,您可以尝试使用 Matrix 包:

I just noticed that your amount and weeek.number actually define a sparseVector, i.e. a vector made mainly of zeroes where just the indices of the non-zero elements is kept. So, you can try with the Matrix package:

require(Matrix)
dt[,list(as.vector(sparseVector(amount,weeek.number,10))),by=ID]

得到与上面相同的结果.

to get the same result as above.

这篇关于展开数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆