Python
guy new to R
, so forgive the naive question.
I have an R
dataframe named metrics
with four columns:
I want to pass the level of aggregation (day
or week
) as a variable to dcast
for aggregation.
agg_level <- c("week")
If I hard-code week
in the in the function, it aggregates data for each week correctly:
met <- dcast(metrics, week ~ city, value.var = count, fun.aggregate = sum)
- Output:
week
NYC
CHI
SF
2015-10-18
1
2
3
2015-10-25
4
5
6
If I replace week
with the variable, it fails. (It aggregates data for all weeks.)
met <- dcast(metrics, agg_level ~ city, value.var = count, fun.aggregate = sum)
Output:
agg_level
NYC
CHI
SF
week
5
7
9
Based on this, metrics[[agg_level]]
extracts a column from variable, but this fails:
met <- dcast(m, [[agg_level]] ~ city, value.var = metric, fun.aggregate = sum)
Error in (function ... unexpected '[['
What is the correct way to do this?