R 将两个字段重塑为边距并计算其交点
我正在尝试重塑sales data.frame,它具有以下结构R 将两个字段重塑为边距并计算其交点,r,reshape,R,Reshape,我正在尝试重塑sales data.frame,它具有以下结构 categorization_one gender created_at fk_id_customer_info CONCAT_gender_cat_one 1 Toys Feminino 13/11/2013 04:54 1 ToysFemale 2 Toys Masculi
categorization_one gender created_at fk_id_customer_info CONCAT_gender_cat_one
1 Toys Feminino 13/11/2013 04:54 1 ToysFemale
2 Toys Masculino 13/11/2013 04:54 2 Toys Male
3 Computers Masculino 14/11/2013 04:54 2 Toys Male
我想像excel数据透视表一样,将行作为分类字段,将列作为字段CONCAT\u gender\u cat\u one中的值。这个表中的值是分类1和分类1的交集之间的计数。
我正尝试使用以下代码对包进行重塑:
cast(compras.parte.1,fk_id_customer_info ~ categorization_one, count, margins = TRUE)
但我得到了一个错误:
incorrect number of dimensions
编辑,这是dput(droplevels(head(compras.parte.1))结果的复制/粘贴
首先,这里是更友好的data.frame形式的数据
dd<-structure(list(categorization_one = structure(c(2L, 2L, 1L), .Label = c("Computers",
"Toys"), class = "factor"), gender = structure(c(1L, 2L, 2L), .Label = c("Feminino",
"Masculino"), class = "factor"), created_at = structure(c(1384336440,
1384336440, 1384422840), class = c("POSIXct", "POSIXt"), tzone = ""),
fk_id_customer_info = c(1L, 2L, 2L), CONCAT_gender_cat_one = structure(c(1L,
2L, 2L), .Label = c("Toys Female", "Toys Male"), class = "factor")), .Names = c("categorization_one",
"gender", "created_at", "fk_id_customer_info", "CONCAT_gender_cat_one"
), row.names = c("1", "2", "3"), class = "data.frame")
谢谢但是我展示的数据只是一个样本,这个领域有600多个因素。如果没有结构和标签参数,我如何计算dd?你的确切意思是什么?你能举一个例子说明你在问题中想要的结果吗?结果正好是你在回答中所展示的结果。我只是觉得计算dd太复杂了,因为您使用标签作为输入(我在某些字段中有600多行),所以我想知道如何在不计算dd的情况下将数据转换为您的输出格式而不使用标签输入。我尝试了您的代码,但在unique中出错。默认值(x):unique()只能应用于向量
dd
只是一个数据帧。多亏了这些空格,你才不容易复制和粘贴上面发布的数据。您的数据应该已经在data.fram中。如果您最初发布了dput(head(yourdata))
,我们就可以知道所有列的数据类型了。这看起来不像dput()
,看起来像是head()
@MrFlick哦,好的,dput()输出了所有的因子,但无法在这里打印,因为fk_id_customer_info有10万个系数(每个客户1个)@user3511563,可能dput(droplevels(head(compras.parte.1))
然后?@MrFlick我用Ananda建议更新了我的问题。
dd<-structure(list(categorization_one = structure(c(2L, 2L, 1L), .Label = c("Computers",
"Toys"), class = "factor"), gender = structure(c(1L, 2L, 2L), .Label = c("Feminino",
"Masculino"), class = "factor"), created_at = structure(c(1384336440,
1384336440, 1384422840), class = c("POSIXct", "POSIXt"), tzone = ""),
fk_id_customer_info = c(1L, 2L, 2L), CONCAT_gender_cat_one = structure(c(1L,
2L, 2L), .Label = c("Toys Female", "Toys Male"), class = "factor")), .Names = c("categorization_one",
"gender", "created_at", "fk_id_customer_info", "CONCAT_gender_cat_one"
), row.names = c("1", "2", "3"), class = "data.frame")
with(dd, table(categorization_one, CONCAT_gender_cat_one))
# CONCAT_gender_cat_one
# categorization_one Toys Female Toys Male
# Computers 0 1
# Toys 1 1