Using columns with special characters in formulae in R

前端 未结 3 915
一向
一向 2021-01-18 02:06

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.

相关标签:
3条回答
  • 2021-01-18 02:52

    I just came across the same problem, and I don't want any change in the name when pass it to R formulae. R allow non-syntactic column names with backticks surrounding them. So I try add backticks to the name and it also works well. My code like below:

    lapply(colnames(variable), function(gene){
    formula0 <- paste0("gleason_grade", "~" "`", gene, "`")
    logit <- clm(as.formula(formula0), data = mydata)
    })
    

    and now you can pass the new variable to formula without error.
    If you don't expect any change to the variable like me, so just backtick it.

    0 讨论(0)
  • 2021-01-18 02:53

    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  
    
    0 讨论(0)
  • 2021-01-18 03:00

    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!

    0 讨论(0)
提交回复
热议问题