r - Add a calculated column to data frame based on another data frame -
i have 2 data frames in r:
- tvnationalsale: each row 1 tv ad placement
- workingnational: each row total web sessions minute
i want add calculated column tvnationalsale contains sum of sessions in 5 minutes prior ad showing. using dplyr package basic formatting.
> glimpse(tvnationalsale) observations: 1443 variables: $ sort.date (fctr) 5/8/2015, 5/8/2015, 5/8/2015, 5/8/2015, 5/8/2015, 5/8/2015, 5/8/2015, 5/8... $ before.time (time) 2015-08-05 06:03:00, 2015-08-05 21:12:00, 2015-08-05 08:49:00, 2015-08-05... $ ad.time (time) 2015-08-05 06:08:00, 2015-08-05 21:17:00, 2015-08-05 08:54:00, 2015-08-05... $ after.time (time) 2015-08-05 06:13:00, 2015-08-05 21:22:00, 2015-08-05 08:59:00, 2015-08-05... $ market.long.desc (fctr) national, national, national, national, national, national, national, nat... $ campaign.name (fctr) europe-sale, europe-sale, europe-sale, europe-sale, europe-sale, europe-s... > glimpse(workingnational) observations: 44616 variables: $ date (date) 2015-05-01, 2015-05-01, 2015-05-01, 2015-05-01, 2015-05-01, 2015-05-01, 2015-05-0... $ hour (fctr) 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... $ minute (fctr) 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,... $ sessions (dbl) 161, 71, 65, 58, 63, 58, 56, 41, 56, 45, 58, 57, 37, 48, 37, 41, 43, 44, 36, 38, 4... $ time (chr) "01:01:00", "01:02:00", "01:03:00", "01:04:00", "01:05:00", "01:06:00", "01:07:00"... $ datetime (time) 2015-05-01 01:01:00, 2015-05-01 01:02:00, 2015-05-01 01:03:00, 2015-05-01 01:04:0... this example shows how calculate period metrics within 1 data frame can't figure out how calculate similar metric separate data frame.
i tried code think doesn't work because trying reference separate data frame in mutate() command.
tvnationalsale <- tvnationalsale %>% mutate(before.sessions=sum(filter(workingnational, datetime>=tvnationalsale$before.time & datetime<=tvnationalsale$ad.time)$sessions)) any ideas on how append calculated metrics data frame?
assuming workingnational data doesn't have gaps or other irregularities, location of each ad time in workingnational , take 5 entries leading time:
indices <- match(tvnationalsale$ad.time, workingnational$datetime) tvnationalsale$fiveminutesbefore <- rowsums(sapply(1:5, function(x) workingnational$sessions[indices-x])) head(tvnationalsale) # ad.time fiveminutesbefore # 1 2015-01-03 04:02:00 3126 # 2 2015-01-05 02:57:00 2221 # 3 2015-01-04 14:53:00 4269 # 4 2015-01-07 01:17:00 1916 # 5 2015-01-06 15:37:00 2484 # 6 2015-01-03 14:23:00 3092 data:
set.seed(144) workingnational=data.frame(datetime=seq(from=isodate(2015, 1, 1), to=isodate(2015, 1, 8), by="min")) workingnational$sessions <- sample(1:1000, nrow(workingnational), replace=true) tvnationalsale=data.frame(ad.time=sample(workingnational$datetime, 100))
Comments
Post a Comment