Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 使用计数重塑数据_R_Reshape2 - Fatal编程技术网

R 使用计数重塑数据

R 使用计数重塑数据,r,reshape2,R,Reshape2,我有一个数据集,我想用R中的包整形2对其进行整形,但我遇到了以下错误: Aggregation function missing: defaulting to length 这是我的数据的head(): cat_one customer valor cama A 1 cama B 1 cama C 1 mesa D 1 mesa A 1 mesa A 1 我想用

我有一个数据集,我想用R中的包整形2对其进行整形,但我遇到了以下错误:

Aggregation function missing: defaulting to length
这是我的数据的head():

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      ...
C
D            ...     ...
这是我的代码:

dcast(dados_teste, cat_one ~ customer, value.var = 'valor')

我遵循另一个,但同样的解决方案对我不起作用。

你把公式的LHS和RHS弄混了

尝试:

您提到的“错误”实际上只是一个警告,告诉您它只是在计算值的数量,而不是应用任何其他函数。所以,在这种情况下,这是完全可以接受的

如果要删除它,请指定
fun.aggregate=length

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

如果只计算了您要查找的两列,您还可以查看

as.data.frame.matrix(table(dados_teste[c(2, 1)]))
#   cama mesa
# A    1    2
# B    1    0
# C    1    0
# D    0    1
as.data.frame.matrix(table(dados_teste[c(2, 1)]))
#   cama mesa
# A    1    2
# B    1    0
# C    1    0
# D    0    1