[英]Time series data manipulation
I've had this problem before, but I didn't write down the solution, so now I'm in trouble again! 我之前遇到过这个问题,但是我没有记下解决方案,所以现在我又遇到了麻烦!
I have a dataframe like the following: 我有一个如下数据框:
Date Product Qty Income
201001 0001 1000 2000
201002 0001 1500 3000
201003 0001 1200 2400
.
.
201001 0002 3500 2000
201002 0002 3200 1900
201003 0002 3100 1850
In words, I have one line for each combination of Date/Product, and the information of Quantity and Income for each combination. 换句话说,每个组合的日期/产品,每个组合的数量和收入信息都有一行。
I want to rearrange this dataframe so it looks like the following: 我想重新排列此数据框,使其如下所示:
Date Qty.0001 Income.0001 Qty.0002 Income.0002
201001 1000 2000 3500 2000
201002 1500 3000 3200 1900
201003 1200 2400 3100 1850
In words, I want to have one line for each date, and one column for each combination of Product/Information(Qty, Income). 换句话说,我希望每个日期都有一行,每个产品/信息组合(数量,收入)有一列。
How can I achieve this? 我怎样才能做到这一点? Thanks in advance!
提前致谢!
Use reshape
: 使用
reshape
:
reshape(x,idvar="Date",timevar="Product",direction="wide")
Date Qty.0001 Income.0001 Qty.0002 Income.0002
1 201001 1000 2000 3500 2000
2 201002 1500 3000 3200 1900
3 201003 1200 2400 3100 1850
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.