I'm working with a data frame of 18 columns, with the working columns being CPM and SpendRange. Spend range is broken up into levels 1:3000 in steps of 50.
我正在使用18列的数据框,工作列是CPM和SpendRange。支出范围分为1:3000,步长为50。
I'm trying to average the the CPM (Cost per Mil) within each spend range and result in a data frame with the unique spend ranges and the mean CPM in each.
我试图在每个支出范围内平均每千次展示费用(每千米成本),并产生一个数据框,其中包含每个支出范围和每个平均每千次展示费用。
I tried:
我试过了:
CPMbySpend<-aggregate(Ads$CPM,by=list(Ads$SpendRange),function(x) paste0(sort(unique(x)),collapse=mean(Ads$CPM))
CPMbySpend<-data.frame(CPMbySpend)
Obviously finding out that I can't use the collapse as a function... any suggestions?
显然发现我不能将崩溃作为一种功能......任何建议?
Optimum output would be:
最佳输出将是:
1-50 | mean(allvalues with spendrange 1-50)
51-100 | mean(allvalues with spendrange 51-100)
1 个解决方案
#1
1
Using the new dataset
使用新数据集
Ads <- read.csv("Test.csv", header=TRUE, stringsAsFactors=FALSE)
Ads$CPM <- as.numeric(Ads$CPM) #the elements that are not numeric ie. `-$` etc. will be coerced to NAs
#Warning message:
#NAs introduced by coercion
res <- aggregate(Ads$CPM,by=list(SpendRange=Ads$SpendRange),FUN=mean, na.rm=TRUE)
If you want to order the SpendRange
i.e 0-1
, 1-50
etc, one way is to use mixedorder
from gtools
.
如果你想订购SpendRange,即0-1,1-50等,一种方法是使用gtools中的mixedorder。
library(gtools)
res1 <- res[mixedorder(res$SpendRange),]
row.names(res1) <- NULL
head(res1)
# SpendRange x
#1 0-1 1.621987
#2 1-50 2.519853
#3 51-100 3.924538
#4 101-150 5.010795
#5 151-200 3.840549
#6 201-250 4.286923
Otherwise, you could change the order by specifying the levels
of SpendRange
by calling factor
.i.e.
否则,您可以通过调用factor.i.e指定SpendRange的级别来更改顺序。
res1$SpendRange <- factor(res1$SpendRange, levels= c('0-1', '1-50',.....)) #pseudocode
and then use
然后使用
res1[order(res1$SpendRange),]
#1
1
Using the new dataset
使用新数据集
Ads <- read.csv("Test.csv", header=TRUE, stringsAsFactors=FALSE)
Ads$CPM <- as.numeric(Ads$CPM) #the elements that are not numeric ie. `-$` etc. will be coerced to NAs
#Warning message:
#NAs introduced by coercion
res <- aggregate(Ads$CPM,by=list(SpendRange=Ads$SpendRange),FUN=mean, na.rm=TRUE)
If you want to order the SpendRange
i.e 0-1
, 1-50
etc, one way is to use mixedorder
from gtools
.
如果你想订购SpendRange,即0-1,1-50等,一种方法是使用gtools中的mixedorder。
library(gtools)
res1 <- res[mixedorder(res$SpendRange),]
row.names(res1) <- NULL
head(res1)
# SpendRange x
#1 0-1 1.621987
#2 1-50 2.519853
#3 51-100 3.924538
#4 101-150 5.010795
#5 151-200 3.840549
#6 201-250 4.286923
Otherwise, you could change the order by specifying the levels
of SpendRange
by calling factor
.i.e.
否则,您可以通过调用factor.i.e指定SpendRange的级别来更改顺序。
res1$SpendRange <- factor(res1$SpendRange, levels= c('0-1', '1-50',.....)) #pseudocode
and then use
然后使用
res1[order(res1$SpendRange),]