【发布时间】:2018-03-07 07:42:45
【问题描述】:
我有一些数据我melt和dcast使用reshape2包,如下。
dat <- data.frame(Name = c("Alice", "Alice", "Alice", "Alice", "Bob", "Bob", "Bob"),
Month = c(1, 1, 1, 2, 1, 2, 2),
Product = c("Car", "Bike", "Car", "Car", "Car", "Bike", "Bike"),
Price = c(1000, 150, 300, 500, 2000, 200, 100))
# Name Month Product Price
# 1 Alice 1 Car 1000
# 2 Alice 1 Bike 150
# 3 Alice 1 Car 300
# 4 Alice 2 Car 500
# 5 Bob 1 Car 2000
# 6 Bob 2 Bike 200
# 7 Bob 2 Bike 100
dat_melt <- melt(dat, id=c("Name", "Month", "Product"))
# Name Month Product variable value
# 1 Alice 1 Car Price 1000
# 2 Alice 1 Bike Price 150
# 3 Alice 1 Car Price 300
# 4 Alice 2 Car Price 500
# 5 Bob 1 Car Price 2000
# 6 Bob 2 Bike Price 200
# 7 Bob 2 Bike Price 100
dat_spread <- dcast(dat_melt, Name + Month ~ Product + variable, value.var="value", fun=sum)
# Name Month Bike_Price Car_Price
# 1 Alice 1 150 1300
# 2 Alice 2 0 500
# 3 Bob 1 0 2000
# 4 Bob 2 300 0
我怎样才能获得此输出,以使名称-月份-产品组合不存在的情况(例如 Alice、2、Bike)返回 NULL 或 NA 而不是 0?请注意,该解决方案应该适用于 Price 为 0 的情况,例如dat_spread$BikePrice[BikePrice == 0] <- NA 是不可接受的。
我曾尝试在dcast 中使用匿名函数,但无济于事,例如
library(dplyr)
dcast(dat_melt, Name + Month ~ Product + variable, value.var="value",
fun.aggregate = function(x) if_else(is.na(x), NULL, sum(x)))
# Error: `false` must be type NULL, not double
dcast(dat_melt, Name + Month ~ Product + variable, value.var="value",
fun.aggregate = function(x) if_else(is.na(x), 3.14, sum(x))) # then update after
# Error in vapply(indices, fun, .default) : values must be length 0,
# but FUN(X[[1]]) result is length 1
注意,reshape2 不是必需的,所以如果您有一个不使用它的解决方案(例如使用 tidyverse 函数),那也很好。
【问题讨论】: