Formulate data for rpart

笑着哭i 提交于 2020-01-17 00:44:11

问题


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.


回答1:


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) 


来源:https://stackoverflow.com/questions/13545391/formulate-data-for-rpart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!