Using columns with special characters in formulae

2019-06-18 23:20发布

I'm trying to make a decision tree using rpart using a data frame that has ~200 columns. Some of these columns have numbers in their names, some have special characters (e.g. "/"). When I try to generate the tree I get error such as the ones below:

R> gg.rpart <- rpart(nospecialchar ~ Special/char, data=temp, method="class")
Error in eval(expr, envir, enclos) : object 'Special' not found
R> gg.rpart <- rpart(nospecialchar ~ "Special/char", data=temp, method="class")
Error in terms.formula(formula, data = data) : invalid model formula in ExtractVars
R> gg.rpart <- rpart(nospecialchar ~ `Special/char`, data=temp, method="class")
Error in `[.data.frame`(frame, predictors) : undefined columns selected

Do I have to change the names to accommodate R or is there some way to pass column names with special characters to R formulae?

2条回答
狗以群分
2楼-- · 2019-06-18 23:40

Joran's comment on my question is the answer - I didn't know of the existence of make.names()

Joran, if you reply as an answer I'll mark you as correct. Cheers!

查看更多
别忘想泡老子
3楼-- · 2019-06-18 23:44

This works:

dat <- data.frame(M=rnorm(10),'A/B'=1:10,check.names=F)

> lm(M~`A/B`,dat)

Call:
lm(formula = M ~ `A/B`, data = dat)

Coefficients:
(Intercept)        `A/B`  
    -1.0494       0.1214  
查看更多
登录 后发表回答