我创建了一个自定义函数,该函数吸收了三个单独的变量:
dblExponential<- function(a, b, x){
# Prepare result data frame
result_df <- data.frame(x)
# loop through the sequence
for (i in a) {
for (j in b){
# Compute column for value i
result_column <- j^(i^(x/max(x)*100))
# Define name of column in the resulting data frame
result_column_name <- as.character(paste(x, i, j, sep = "_"))
# Add the column to the data frame
result_df[result_column_name] <- result_column
}
}
# Return the result data frame
return(result_df)
}
我想通过它运行以下数据框:
dfTest<- data.frame(rnorm(10,13,3), rnorm(10, 12, 2))
colnames(dfTest)<- c("var1", "var2")
# creating a & b per the function above
alpha<- seq(0.85, 0.95, by= .01)
beta<- sapply(1:10, function(x){.1**x})
# run function
dfTest2<- dblExponential(alpha, beta, dfTest)
当我直接通过该函数传递dfTest
时,我会得到220个所需的变量(每个var
的alpha值通过11个值,beta值为10个值),但是名称在格式如下:
c(7.74041000068398, 17.0484482255766, 14.3454072552564,
11.9569186059367, 15.8419904194506, 20.8602917396248, 11.8515604771538,
10.5655772517175, 13.32365521973, 15.9976857032445)_0.95_1e-08
我需要在paste
函数中进行什么更改才能对此进行调整?
此外,我正在尝试弄清这是否值得使用dplyr
,并且当我尝试通过转换运行它时,会出现另一个错误:
dfTest3<- dfTest %>%
transmute_all(funs(sCurve(alpha, beta, dfTest)))
它返回以下错误:
> Error in mutate_impl(.data, dots) :
> Column `var1` is of unsupported class data.frame
我想知道如何同时使用两者以提高灵活性。