如何在 R 中将 3 维数据转换为 2d?
How to convert 3 dimension data into 2d in R?
我有沃尔玛超市的销售数据。
它有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
是第 1 个月的值,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
。
.
.
原始数据结构
应用 melt、transform 和 cast 函数后的数据结构
这出乎我的意料。
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
注意:如果出现错误 "Aggregation function missing: defaulting to length",请通过 'unique' 函数删除重复项或添加聚合参数。
我有沃尔玛超市的销售数据。
它有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
是第 1 个月的值,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
。 . .
原始数据结构
应用 melt、transform 和 cast 函数后的数据结构
这出乎我的意料。
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
注意:如果出现错误 "Aggregation function missing: defaulting to length",请通过 'unique' 函数删除重复项或添加聚合参数。