r - dcast restructuring from long to wide format not working -
my df
looks this:
id task type freq 3 1 2 3 1 b 3 3 2 3 3 2 b 0 4 1 3 4 1 b 3 4 2 1 4 2 b 3
i want restructure id , get:
id b … z 3 5 3 4 4 6
i tried:
df_wide <- dcast(df, id + task ~ type, value.var="freq")
and got error:
aggregation function missing: defaulting length
i can't figure out put in fun.aggregate
. what's problem?
the reason why getting warning in description of fun.aggregate
(see ?dcast
):
aggregation function needed if variables not identify single observation each output cell. defaults length (with message) if needed not specified
so, aggregation function needed when there more 1 value 1 spot in wide dataframe.
an explanation based on data:
when use dcast(df, id + task ~ type, value.var="freq")
get:
id task b 1 3 1 2 3 2 3 2 3 0 3 4 1 3 3 4 4 2 1 3
which logical because each combination of id
, task
, type
there value in freq
. when use dcast(df, id ~ type, value.var="freq")
(including warning message):
aggregation function missing: defaulting length id b 1 3 2 2 2 4 2 2
now, looking @ top part of data:
id task type freq 3 1 2 3 1 b 3 3 2 3 3 2 b 0
you see why case. each combination of id
, type
there 2 values in freq
(for id 3: 2
, 3
a
& 3
, 0
type b
) while can put 1 value in spot in wide dataframe each values of type
. therefore dcast
wants aggregate these values 1 value. default aggregation function length
, can use other aggregation functions sum
, mean
, sd
or custom function specifying them fun.aggregate
.
for example, fun.aggregate = sum
get:
id b 1 3 5 3 2 4 4 6
now there no warning because dcast
being told when there more 1 value: return sum of values.
Comments
Post a Comment