I've been trying to use the dcast()
function in reshape2
to widen a large dataframe in R. However, I am not sure what to use for the aggregation function, fun.aggregate
that dcast
requires because I want to keep the discrete values of the value.var
, whereas dcast
insists on forcing length
as the default, making every value dichotomous. For illustration, my data look like this:
x <- c("a", "b", "c")
y <- c("d", "e", "f")
num <- c(10, 20, 21)
data <- data.frame(cbind(x,y,num))
x y num
a d 10
b e 20
c f 21
After input m <- dcast(data, x ~ y, value.var = "num")
, dcast
returns the following DF:
d e f
a 1 0 0
b 0 1 0
c 0 0 1
However, I want it to look like this:
d e f
a 10 0 0
b 0 20 0
c 0 0 21
What am I doing wrong?