Reshaping data with count [duplicate]

ε祈祈猫儿з 提交于 2019-11-29 11:47:40

You've mixed up the LHS and RHS of the formula.

Try:

library(reshape2)
dcast(dados_teste, customer ~ cat_one, value.var = "valor")
# Aggregation function missing: defaulting to length
#   customer cama mesa
# 1        A    1    2
# 2        B    1    0
# 3        C    1    0
# 4        D    0    1

The "error" that you refer to is actually just a warning that tells you that it is just counting the number of values--not applying any other function. So, in this case, it's perfectly acceptable.

If you want to get rid of it, specify fun.aggregate = length.

dcast(dados_teste, customer ~ cat_one, 
      value.var = "valor", fun.aggregate = length)

If its just counts of two columns that you're after, you could also look at table:

as.data.frame.matrix(table(dados_teste[c(2, 1)]))
#   cama mesa
# A    1    2
# B    1    0
# C    1    0
# D    0    1
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!