paste("a", letters[2:26], sep = "*") |>
paste(collapse = " + ") |>
sprintf(fmt = "Y ~ %s") |>
as.formula()
##> Y ~ a * b + a * c + a * d + a * e + a * f + a * g + a * h + a *
##> i + a * j + a * k + a * l + a * m + a * n + a * o + a * p +
##> a * q + a * r + a * s + a * t + a * u + a * v + a * w + a *
##> x + a * y + a * z
# this would be the columns of a dataframe
effects_list = ['regressor_col','A', 'B', 'C', 'D', 'E','F']
interaction = effects_list[3]
regressor = effects_list[0]
formula = regressor + ' ~'
for effect in effects_list:
# check if it's the interaction term if it is skip it
#print((effect != interaction) & (effect != regressor))
if (effect != interaction) & (effect != regressor):
formula = formula + ' + ' + effect + '*' + interaction
print(formula)
3条答案
按热度按时间yhuiod9q1#
例如
xlpyo6sf2#
下面的示例说明了如何构造所需的字符串,然后将其转换为公式
iih3973s3#
解决方案使用正则表达式: