I'm trying to aggregate revenue data by day. The column contains both NAs and actual revenue data intermingled. Revenue is currently a 'character.' The challenge is that you cannot sum 'characters' and any attempt to change the class breaks.
Changing class via as.numeric
yields this error: "NAs introduced by coercion"
or as.numeric(as.character())
completely wipes the data in that column and returns all NAs.
Date column is in date format. Here's the aggregation I'm trying to execute.
df_agg<-aggregate(df$revenue,by=list(df$Date),sum,na.rm=TRUE)
Executing the above code of course results in: "Error in Summary.factor(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, : ‘sum’ not meaningful for factors"
The data is originally read via CSV. I've tried reading both with/without stringsAsFactors=FALSE
Example code
Code Date Visits Revenue
1A 1/1/2018 3 5
2A 1/1/2018 5 NA
3A 1/2/2018 8 7
4A 1/3/2018 6 2
5A 1/3/2018 8 5