【问题标题】:reshaping 2 fields as margins and counting their intersection将 2 个字段重塑为边距并计算它们的交集
【发布时间】:2014-07-18 21:35:03
【问题描述】:

我正在尝试重塑具有这种结构的销售 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          Masculino     13/11/2013 04:54         2                   Toys Male
3 Computers         Masculino      14/11/2013 04:54         2                   Toys Male

我想做一个 excel 数据透视表,其中行是 categorization_one 字段,列是字段 CONCAT_gender_cat_one 内的值。此表中的值将是 categorization_one 和 CONCAT_gender_cat_one 的交集之间的计数。 我正在尝试使用以下代码使用 reshape 包来做到这一点:

cast(compras.parte.1,fk_id_customer_info ~ categorization_one, count, margins = TRUE)

但是我收到了这个错误:

incorrect number of dimensions

编辑,这是来自以下结果的复制/粘贴:dput(droplevels(head(compras.parte.1)))

structure(list(categorization_one = structure(c(4L, 2L, 3L, 2L, 
1L, 5L), .Label = c("Bebês/Alimentação/Mamadeiras", "Brinquedos/Desenhos e Pintura", 
"Brinquedos/Games e Eletrônicos/Laptops, Tablets e Cia", "Brinquedos/Primeira Infância", 
"Calçados/Sapatilhas"), class = "factor"), gender = structure(c(1L, 
2L, 2L, 2L, 3L, 1L), .Label = c("Feminino", "Masculino", "Masculino/Feminino"
), class = "factor"), created_at = structure(c(1L, 1L, 1L, 1L, 
1L, 1L), .Label = "13/11/2013 04:54", class = "factor"), fk_id_customer_info = structure(c(1L, 
2L, 2L, 2L, 3L, 1L), .Label = c("2", "3", "5"), class = "factor"), 
    GENDER_PESQUISA = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "não respondeu", class = "factor"), 
    cat_one_gender = structure(c(4L, 2L, 3L, 2L, 1L, 5L), .Label = c("Bebês/Alimentação/MamadeirasMasculino/Feminino", 
    "Brinquedos/Desenhos e PinturaMasculino", "Brinquedos/Games e Eletrônicos/Laptops, Tablets e CiaMasculino", 
    "Brinquedos/Primeira InfânciaFeminino", "Calçados/SapatilhasFeminino"
    ), class = "factor")), .Names = c("categorization_one", "gender", 
"created_at", "fk_id_customer_info", "GENDER_PESQUISA", "cat_one_gender"
), row.names = c(NA, 6L), class = "data.frame")

【问题讨论】:

  • 这看起来不像dput(),看起来只是head()
  • @MrFlick 哦好的,dput() 输出所有因子,但是这里无法打印,因为 fk_id_customer_info 有 100k 个因子(每个客户 1 个)
  • @user3511563,然后可能是dput(droplevels(head(compras.parte.1)))
  • @MrFlick 我用 Ananda 的建议更新了我的问题。

标签: r reshape


【解决方案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")

那么听起来你只是想要一个简单的表格

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

【讨论】:

  • 谢谢!但我展示的数据只是一个样本,categorization_one 字段有 600 多个因子。没有结构和标签参数如何计算 dd?
  • 你到底是什么意思?您能否举例说明您希望在问题中得到的输出?
  • 输出正是您在答案中显示的输出。我只是认为计算 dd 太复杂了,因为您使用标签作为输入(我在某些字段中有超过 600 行),所以只想知道如何在不计算 dd 的情况下从我的数据转到输出格式标签输入
  • 我尝试了您的代码,但在 unique.default(x) 中出现错误:unique() 只能应用于向量
  • dd 只是一个data.frame。由于空格,您无法轻松复制和粘贴您在上面发布的数据。您的数据应该已经在 data.fram 中。如果您最初发布 dput(head(yourdata)) 会更容易,这样我们就可以知道所有列的数据类型。
猜你喜欢
  • 2019-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多