Question

Concatenate columns name of a list to prepare a formula for rpart?

Just wanted to concatenate the names(log_data), log_data is a list of 60 vectors distinct vectors, so I just want their column names in a format so that I can put them in a formula of rpart in r..... like rpart(A ~ B + C + D + E ,log_data), so here I just want to extract formula="A~B+C+D+E" as a whole string where A,B,C,D,E are the columns name which we have to extract from the log_data, or is there any better way to get a tree from the list.
I have tried,

a <- names(log_data)  
rpart(a[1] ~ a[2] + a[3] + a[4], log_data)

getting an error

Error in paste(temp, yprob[, i], sep = " ") : subscript out of bounds

where

a[2]

[1] "X.u.crpice..vin20f1..vol.vin20f1v1.r_credit_credshare2...91...90."

a[3]

[1] "X.u.crpice..vin20f1..vol.vin20f1v1.r_credit_credshare2...92...90."

c<-paste(a[1], "~", sep="")

rpart_formula <- as.formula(paste(c, paste(a[2:60], collapse = " + "), sep = ""))

rpart(rpart_formula,log_data)

it is going in infinite loop at rpart just because of too long column name or may be n=60

Can I attach any column names colnames(log_data) <- c(?), what should I put at "?", so that will be easy to draw it for n=60.

Was it helpful?

Solution

I believe you want

shortnames <- paste0("c",seq(ncol(log_data)))
names(log_data) <- shortnames
form <- reformulate(paste(shortnames[2:4],collapse="+"),
                    response=shortnames[1])
rpart(form,log_data) 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top