我有来自沃尔玛(如超市)的销售数据。
它有 5 个变量:
ProductId, Category, Mode of Operations, Sales, Profit Margin
这 5 个变量中有两个是唯一的,即
ProductId and Category
。其他 3 个可以每月更改其值。数据为 23 个月。
所以,如果我有一个产品,生活就会很简单,而且它会是一个二维数据。但由于我的产品数量超过 20,000 个,我有点困惑如何处理这些数据,以便我可以运行 linear regression
, cart
以及其他功能。
编辑1:示例
read.table(text="
A B C1 D1 E1 C2 D2 E2 C3 D3 E3 C4 D4 E4 C5 D5 E5 C6 D6 E6
1 K X 5 5 X 6 7 Y 2 1 Z 0 0 X 6 7 X 7 9
2 L Y 5 4 X 6 9 Z 2 3 Z 0 0 X 6 6 X 7 10
3 M X 5 5 Z 6 7 X 2 1 Y 0 0 Y 6 7 Y 7 9",
header=TRUE)
C1,D1,E1
是第一个月的值,C2,D2,E2
是第二个月的值,依此类推。
我希望它成为
A B C D E MONTH
1 K X 5 5 1
1 K X 6 7 2
1 K Y 2 1 3
1 K Z 0 0 4
1 K X 6 7 5
1 K X 7 9 6
2 L Y 5 4 1
2 L X 6 9 2
。 。 .
原始数据的结构
应用熔化、变换和转换函数后的数据结构
这不是我所期待的。
A 下应有 4 个级别(即合资企业、经销商、分销、0)
B下应该有价格
在C下应该有利润率,在我的数据中总是小数点后0.xxx
我做错了什么?
编辑3
dcast 函数不会从值列中选取值来分配给变量 A、B 和 C。它从哪里获取所有这些 1 并将其放在 A、B 和 C 下?
> names(wushang_transformed2)
[1] "Productcode" "Category" "CAT" "Month" "value"
> wushang_casted = dcast(wushang_transformed2, Productcode+Category+Month~CAT, value.var="value")
这个命令有什么问题?
最佳答案
# Read in the data
read.table(text="
A B C1 D1 E1 C2 D2 E2 C3 D3 E3 C4 D4 E4 C5 D5 E5 C6 D6 E6
1 K X 5 5 X 6 7 Y 2 1 Z 0 0 X 6 7 X 7 9
2 L Y 5 4 X 6 9 Z 2 3 Z 0 0 X 6 6 X 7 10
3 M X 5 5 Z 6 7 X 2 1 Y 0 0 Y 6 7 Y 7 9",
header=TRUE) -> my_df
#Load required library
library(reshape2)
# melt brings columns not in the id.vars list into variable and value
# where variable is the original column name
my_df_melted <- melt(my_df, id.vars=c("A", "B"))
# transform adds new columns, subtr() splits "C1" into "C" and "1"
# the [,-3] at the end drops the 3rd column
my_df_transformed <- transform(my_df_melted, CAT=substr(variable, 1, 1), MONTH=substr(variable, 2, 2))[,-3]
# dcast keeps A, B, and MONTH columns and pushes CAT into new columns
# [,c(1:2,4:6,3)] reorders the columns
my_df_casted <- dcast(my_df_transformed, A+B+MONTH~CAT)[,c(1:2,4:6,3)]
A B C D E MONTH
1 1 K X 5 5 1
2 1 K X 6 7 2
3 1 K Y 2 1 3
4 1 K Z 0 0 4
5 1 K X 6 7 5
6 1 K X 7 9 6
7 2 L Y 5 4 1
8 2 L X 6 9 2
9 2 L Z 2 3 3
10 2 L Z 0 0 4
11 2 L X 6 6 5
12 2 L X 7 10 6
13 3 M X 5 5 1
14 3 M Z 6 7 2
15 3 M X 2 1 3
16 3 M Y 0 0 4
17 3 M Y 6 7 5
18 3 M Y 7 9 6
注意:如果出现错误“聚合函数缺失:默认为长度”,请通过“唯一”函数删除重复项或添加聚合参数。
关于r - 如何在R中将3维数据转换为2D?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31932916/