ayol
ayol

Reputation: 13

Creating a data frame of predictors and a factor vector

In R-package 'caret' a dataset mdrr contains a dataframe with predictors and a vector factor mdrrClass.

require(caret)
data(mdrr)

How do I create a similar format for my own dataset where Pred1, Pred2, Pred3 in a data frame while the corresponding 'class' as factor? Say

x = data.frame(id = c("a","c","d","g"), 
          Pred1 = c(1,3,4,7),  Pred2 = c(1,3,4,7),  
          Pred3 = c(1,3,4,7),  
          class = c(1,3,4,7))

Thank you.

Upvotes: 1

Views: 187

Answers (1)

Jthorpe
Jthorpe

Reputation: 10194

When you call data(mdrr), R loads the file named "mdrr.Rda" which contains two objects, a data.frame (mdrrDescr), and an ordinary factor (mdrrClass). There's nothing particularly special about either of these objects (except maybe that the length of the factor is the same as the number of rows in the data.frame).

If you want to create a package for which data('myData') loads two objects (say,'a' and 'b'), just save those objects to a single .Rda file:

save(a=myFavoriteDataFrame,
     b=myFavoriteFactor,
     file='path/to/my/package/data/myData.Rda')

Upvotes: 1

Related Questions