Hi I have a data looks like this:

partNumber manufacturer supplier availability
p1         m1           s1       100
p1         m1           s2       500
p2         m2           s1       200
p2         m2           s3       300
....

My expected output should look like this:

partNumber manufacturer s1    s2    s3...
p1         m1           100   500   0 ...
p2         m2           200   0     300 ...
...

I was trying to implement this using dcast in reshape2 package and this is my command:

df.dcast <- dcast(df, partNumber + manufacturer ~ supplier, value.var="availability")

However, the output value is only 0 and 1, seems like it turned output to be a checkout instead of displaying the availability value.

partNumber manufacturer s1    s2    s3...
p1         m1           1     1     0 ...
p2         m2           1     0     1 ...
...

btw, while I run the command, there is a warning that

Aggregation function missing: defaulting to length... 

I don't quite understand what does the aggregation function do and any help and explanation would be apprecited.

有帮助吗?

解决方案

When I do this with your data example I get:

df.dcast <- dcast(df, partNumber + manufacturer ~ supplier, value.var="availability")
df.dcast
  partNumber manufacturer  s1  s2  s3
1         p1           m1 100 500  NA
2         p2           m2 200  NA 300

What's most likely happening is that your availability column is not what you think it is. Perhaps it got converted to a factor variable because there was some text in that column in the input file. If that's the case then you need to read R-FAQ 7.10

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top