英文:
error as.formula function: attempt to set an attribute on NULL
问题
labels <- paste('label', seq(1, 8, 1), sep='', collapse = ' + ')
labels <- paste(labels, '+', 'label9')
labels <- as.formula(labels)
Error in class(ff) <- "formula" : attempt to set an attribute on NULL
What I need to get is label1 + label2 + label3 + label4 + label5 + label6 + label7 + label8 + label9 as a formula.
英文:
A quick question:
I'm building a NN model and I don't want to type in all the variables. Instead I'd like to save a variable with some of the col names and use it int he formula.
When I'm approaching it in this way, this code gives me an error.
Why and how can I fix it?
labels <- paste('label',seq(1,8,1), sep='', collapse = ' + ')
labels <- paste(labels,'+','label9')
labels <- as.formula(labels)
Error in class(ff) <- "formula" : attempt to set an attribute on NULL
What I need to get is label1 + label2 + label3 + label4 + label5 + label6 + label7 + label8 + label9 as a formula.
答案1
得分: 2
We may use paste
with ~
as.formula(paste0(labels, "~ ."))
label1 + label2 + label3 + label4 + label5 + label6 + label7 +
label8 + label9 ~ .
Or with reformulate
reformulate(response = labels, ".")
label1 + label2 + label3 + label4 + label5 + label6 + label7 +
label8 + label9 ~ .
英文:
We may use paste
with ~
as.formula(paste0(labels, "~ ."))
label1 + label2 + label3 + label4 + label5 + label6 + label7 +
label8 + label9 ~ .
Or with reformulate
reformulate(response = labels, ".")
label1 + label2 + label3 + label4 + label5 + label6 + label7 +
label8 + label9 ~ .
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论