reshape2 dcast非二进制运算符的非数字参数

时间:2017-04-21 01:12:18

标签: r reshape2 dcast

我正在尝试使用ref.child("United States").child("San Francisco").on("child_added"... 中的dcast将数据框从长整数转换为宽。

我创建了一个这样的数据框:

reshape2

得到,

> Person=c("A","A","A","A","B","B","C","C","C","C")
> Object=c("car", "watch", "bike", "phone","car","skateboard","car","bike","motorcycle")
> Value=c("1","1","1","1","1","1","1","1","1","1")
> df=data.frame(Person, Object, Value)

然后,用dcast

> df
   Person     Object Value
1       A        car     1
2       A      watch     1
3       A       bike     1
4       A      phone     1
5       B        car     1
6       B skateboard     1
7       C        car     1
8       C       bike     1
9       C motorcycle     1
10      C         TV     1

为什么dcast还需要Person和Object列的数字参数?

我正在尝试获得此输出:

> library(reshape2)
> dcast(df, Person + Object, variable.var="Value", fun.aggregate=length)
Error in Person + Object : non-numeric argument to binary operator

*编辑* @neilfws找到了解决方案:

Person    car    watch    bike    phone    skateboard    motorcycle    TV
     A      1        1       1        1             0             0     0
     B      1        0       0        0             1             0     0
     C      1        0       1        0             0             1     1

但是,从制表符分隔的文件导入数据框时:

dcast(df, Person ~ . + Object, variable.var="Value")

相同的dcast命令返回:

df = read.table("Person_Object.tab", header=T, sep="\t")

2 个答案:

答案 0 :(得分:0)

显示如何填充空单元格。

Person=c("A","A","A","A","B","B","C","C","C","C")
Object=c("car", "watch", "bike", "phone","car","skateboard","car","bike","motorcycle", "TV")
Value=c("1","1","1","1","1","1","1","1","1","1")

# note the extra parameter StringsAsFactors - needed so that "0" can be used 
df=data.frame(Person, Object, Value, stringsAsFactors = FALSE)

library(reshape2)
dcast(df, Person ~ Object, value.var="Value", fill = "0")

#   Person bike car motorcycle phone skateboard TV watch
# 1      A    1   1          0     1          0  0     1
# 2      B    0   1          0     0          1  0     0
# 3      C    1   1          1     0          0  1     0
> 

答案 1 :(得分:0)

我们可以使用tidyverse

library(tidyr)
df %>% 
    spread(Object, Value, fill = 0)
#   Person bike car motorcycle phone skateboard TV watch
#1      A    1   1          0     1          0  0     1
#2      B    0   1          0     0          1  0     0
#3      C    1   1          1     0          0  1     0