r - Aggregate by paymentamount -
i have data this:
emailaddress customer_acquisation_date customer_order_date payment_amount xy@gmail.com 01/05/2013 6:24 01/05/2013 5:10 $ 20.67 xy@gmail.com 01/05/2013 6:24 02/07/2013 7:21 pm $ 25.56 xy@gmail.com 01/05/2013 6:24 07/10/2013 8:00 $100.00 xy@gmail.com 01/05/2013 6:24 08/12/2013 9:35 $30.00
i trying sum(payment amount) emailaddress want final output as:
emailaddress customer_acquisation_date customer_order_date payment_amount xy@gmail.com 01/05/2013 6:24 01/05/2013 $ 177 02/07/2013 07/10/2013 08/12/2013
code writing
z <- aggregate(x$emailaddress~x$paymentamount,data=x,fun=sum)
error getting
error in summary.factor(c(211594l, 291939l, 79240l, 208971l, 369325l, : ‘sum’ not meaningful factors
what right way of doing this. appreciated
the aggregate function first takes value aggregate on, grouping argument. mentioned, need remove dollar sign able convert column numeric format.
# remove dollar sign x$payment_amount = as.numeric( gsub('[$]', '', x$payment_amount )) # write in right order .. aggregate(x, by, fun .. ) z <- aggregate( payment_amount ~ emailaddress, data = x, fun = sum )
edit: adding data.table solution, keeping other columns well.
library(data.table) setdt(x) # convert data.frame data.table z = x[, payment_total := sum(payment_amount), = emailaddress] setdf(z) # convert result data.frame
Comments
Post a Comment