I have a dataset and I want to reshape it with package reshape2 from R, but I'm getting this error:
Aggregation function missing: defaulting to length
cat_one customer valor
cama A 1
cama B 1
cama C 1
mesa D 1
mesa A 1
mesa A 1
customer cama mesa
A 1 0
B 2 ...
D ... ...
dcast(dados_teste, cat_one ~ customer, value.var = 'valor')
You've mixed up the LHS and RHS of the formula.
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
as.data.frame.matrix(table(dados_teste[c(2, 1)])) # cama mesa # A 1 2 # B 1 0 # C 1 0 # D 0 1