Probably a basic question.
I have a key - value
data.frame
(df
below):
features <- paste0("f",1:5)
set.seed(1)
ids <- paste0("id",1:10)
df <- do.call(rbind,lapply(ids,function(i){
data.frame(id = i, feature = sample(features,3,replace = F))
}))
And I want to tidyr::spread
or reshape2::dcast
it, so that the rows are id' the columns are
feature, but the values are the sum of
featuresfor each
id`.
A simple:
reshape2::dcast(df, id ~ feature)
Doesn't achieve that. It just fills in with feature
s and NA
s
adding fun.aggregate = sum
to the command above results with an error:
> reshape2::dcast(df, id ~ feature, fun.aggregate = sum)
Using feature as value column: use value.var to override.
Error in .fun(.value[0], ...) : invalid 'type' (character) of argument
And tidyr::spread also results with an error:
tidyr::spread(df, key = id, value = feature)
Error: Each row of output must be identified by a unique combination of keys.
Keys are shared for 30 rows:
Any idea?