多个变量值可用于函数和绑定结果 [英] Multiple Variable Values to Function and Cbind Results
问题描述
这是此问题的继续: 具有现有For-loop by行的For-Loop按列
This is a continuation of this question here: For-Loop By Columns with existing For-loop by Rows
我有一个数据集,其中使用了3个变量:
adstock_rate
,diminishing_rate
和lag_number
.
目前,每个号码仅设置为1个号码.
I have a dataset in which I am using 3 variables:
adstock_rate
, diminishing_rate
, and lag_number
.
These are currently set to only 1 number each.
当前我正在使用以下数字:
Currently I am using the following numbers:
adstock_rate<-0.5
lag_number<-1
diminishing_rate<-0.6
最终输出是使用以下函数将新列附加到现有数据集的数据集.
The final output is a dataset with new columns attached to the existing dataset using the function below.
foo1 <- function(dot, lag_val = 1) {
tmp <- dot
for(i in (1 + lag_val): length(tmp)) {
tmp[i] <- tmp[i] + adstock_rate * diminishing_rate * tmp[i - lag_val]
}
return(tmp)
}
advertising_dataset %>%
group_by(Region) %>%
mutate_all(funs(adstocked = foo1(., lag_val = lag_number)))
这就是我想要做的:
我想将此函数应用于这些变量的不同值.以下是这些变量的组合:
I want to apply this function to different values to these variables. Below are the combinations of these variables:
adstock_rate = c(0.50, 0.60, 0.70)
lag_number = c(0,1)
diminishing_rate = c(0.50, 0.60)
combos<-expand.grid(adstock_rate,lag_number,diminishing_rate)
colnames(combos)[1]<-"AdStock_Rate"
colnames(combos)[2]<-"Lag_Number"
colnames(combos)[3]<-"Diminish_Rate"
head(combos)
AdStock_Rate Lag_Number Diminish_Rate
1 0.5 0 0.5
2 0.6 0 0.5
3 0.7 0 0.5
4 0.5 1 0.5
5 0.6 1 0.5
6 0.7 1 0.5
7 0.5 0 0.6
8 0.6 0 0.6
9 0.7 0 0.6
10 0.5 1 0.6
我认为您将必须进行for循环或使用apply函数来降低组合数据集中的行列表.
I think you would have to make a for-loop or use the apply function to go down the list of rows in the combos dataset.
这是我的尝试:
for(j in combos){
foo1 <- function(dot, lag_val = 1) {
tmp <- dot
for(i in (1 + lag_val): length(tmp)) {
tmp[i] <- tmp[i] + combos[j,1] * combos[j,3] * tmp[i - lag_val]
}
return(tmp)
}
advertising_dataset %>%
group_by(Region) %>%
mutate_all(funs(adstocked = foo1(., lag_val = combos[j,2])))
##cbind to previous output
}
我还需要列名称具有数字值,例如adstock_0.5_1_0.6
,其中0.5 =广告存货率,1 =滞后数和递减= 0.6.
I also need the column names to have the number values such as adstock_0.5_1_0.6
where 0.5 = adstock rate, 1 = lag number, and diminishing = 0.6.
希望这是有道理的.
如果您需要我提供更多信息,请告诉我.
Please let me know if you need me to provide any more info.
谢谢!
推荐答案
当我们循环浏览'combos'的行时,创建一个list
,其length
与'combos的行数相同',用于存储for
循环的输出
As we are looping through the rows of 'combos', create a list
that have the same length
as the number of rows of the 'combos' for storing the output from the for
loop
lst <- vector("list", nrow(combos)) # initialize a list to store output
在'foo1'中添加更多参数以提高灵活性
Add some more parameters in the 'foo1' for more flexibility
foo1 <- function(dot, lag_val = 1, combos, ind) {
tmp <- dot
for(i in (1 + lag_val): length(tmp)) {
tmp[i] <- tmp[i] + combos[ind,1] * combos[ind,3] * tmp[i - lag_val]
}
return(tmp)
}
然后循环遍历"combos"行
and then loop through the rows of 'combos'
for(j in seq_len(nrow(combos))){
# assign the group by recursive output to each `list` element
lst[[j]] <- advertising_dataset %>%
group_by(Region) %>%
mutate_all(funs(adstocked =
foo1(., lag_val = combos[j,2], combos, ind = j)))
}
lst
尚不清楚我们是否需要list
名称为'adstock_Rate_Number_Drate'.如果是那样的话,
It is not clear whether we need the list
names to be 'adstock_Rate_Number_Drate' or not. If that is that case,
names(lst) <- paste0("adstock_", do.call(paste, c(combos, sep="_")))
将data.frame
的list
转换为具有"id"列以指示组合的单个data.frame
Convert the list
of data.frame
to a single data.frame having an 'id' column to indicate the combination
out <- bind_rows(lst, .id = 'id')
head(out, 3)
# A tibble: 3 x 6
# Groups: Region [1]
# id Region advertising advertising2 advertising_adst… advertising2_ads…
# <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
#1 adstock_… 500 118. 43.9 147. 54.9
#2 adstock_… 500 120. 231. 150. 289.
#3 adstock_… 500 126. 76.8 157. 96.0
这篇关于多个变量值可用于函数和绑定结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!