Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R中使用很长的字符串创建公式_R_Formula - Fatal编程技术网

在R中使用很长的字符串创建公式

在R中使用很长的字符串创建公式,r,formula,R,Formula,我所处的情况是,对于一个非常大的数据帧,我有一个充满列名的向量 让我们假设:x=c(“姓名”、“地址”、“性别”、“类别”)[大约100个变量] 现在,我想创建一个公式,我最终将使用它创建一个HoeffdingTree。 我使用以下公式创建公式: myformula <- as.formula(paste("class ~ ", paste(x, collapse= "+"))) myformula您可以先减少数据集 dat_small <- dat[,c("class",x)]

我所处的情况是,对于一个非常大的数据帧,我有一个充满列名的向量

让我们假设:
x=c(“姓名”、“地址”、“性别”、“类别”)
[大约100个变量]

现在,我想创建一个公式,我最终将使用它创建一个
HoeffdingTree
。 我使用以下公式创建公式:

myformula <- as.formula(paste("class ~ ", paste(x, collapse= "+")))

myformula您可以先减少数据集

dat_small <- dat[,c("class",x)]

dat_small问题在于,您将R关键字作为列名
else
是一个关键字,因此不能将其用作常规名称

一个简化的例子:

s <- c("x", "else", "z")
f <- paste("y~", paste(s, collapse="+"))
formula(f)
# Error in parse(text = x) : <text>:1:10: unexpected '+'
# 1: y~ x+else+
#              ^

s您可以尝试
重新格式化

 reformulate(setdiff(x, 'class'), response='class')
 #class ~ Name + address + Gender
“x”在哪里

  x <- c("Name", "address", "Gender", 'class')
你有一个名为“else”的专栏吗?它似乎抛出了一个错误:
as.formula(粘贴(“class~”、粘贴(c(“or”、“else”)、折叠=“+”))
解析错误(text=x,keep.source=FALSE)::1:13:意外的“else”1:class~或+else
 reformulate(setdiff(x, 'class'), response='class')
 #class ~ Name + address + Gender
  x <- c("Name", "address", "Gender", 'class')
   reformulate('.', response='class')
   #class ~ .