R summing row one with all rows -


i trying analyse website data ab testing. reference point based on experimentname = experiment 1 (control version)

  experimentname uniquepageview uniquefrequency nonuniquefrequency 1   experiment 1            459             294                359 2   experiment 2            440             286                338 3   experiment 3            428             273                348 

what need sum every uniquepageview, uniquefrequency , nonuniquefrequency row when experimentname = experiment 1

e.g.

uniquepageview experimentname = 'experiment 1 ' +  uniquepageview experimentname = 'experiment 2 ',  uniquepageview experimentname = 'experiment 1 ' +  uniquepageview experimentname = 'experiment 3 ' 

so on forth (i have unlimted number of experiment #) same uniquefrequency , nonuniquefrequency (i have unlimited number of column well)

result expected:

experimentname  uniquepageview  uniquefrequency nonuniquefrequency  conversion rate pooled uniquepageview   conversion rate pooled uniquefrequency  conversion rate pooled nonuniquefrequency 1   experiment 1    459 294 359 918 588 718 2   experiment 2    440 286 338 899 580 697 3   experiment 3    428 273 348 887 567 707 

here math behind it:

    experimentname  uniquepageview  uniquefrequency nonuniquefrequency       conversion rate pooled uniquepageview  conversion rate pooled uniquefrequency  conversion rate pooled nonuniquefrequency 1   experiment 1    459 294 359 459 + 459   294 + 294   359 + 359 2   experiment 2    440 286 338 459 + 440   294 + 286   359 + 338 3   experiment 3    428 273 348 459 + 428   294 + 273   359 + 348 

in base r, can in 1 line column binding (with cbind) initial data frame initial data frame plus version duplicates of "experiment 1" row).

cbind(dat, dat[,-1] + dat[rep(which(dat$experimentname == "experiment 1"), nrow(dat)), -1]) #   experimentname uniquepageview uniquefrequency nonuniquefrequency uniquepageview uniquefrequency # 1   experiment 1            459             294                359            918             588 # 2   experiment 2            440             286                338            899             580 # 3   experiment 3            428             273                348            887             567 #   nonuniquefrequency # 1                718 # 2                697 # 3                707 

to update column names @ end (assuming stored resulting data frame in res), use:

names(res)[4:6] <- c("combinedpageview", "combineduniquefrequency", "combinednonuniquefrequency") 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -