Extract data frame using model.frame and formula

I want to extract a data frame using a formula that determines which columns to select, and some intersect between columns .

I know the function model.frame . However, it does not give me transitions:

For instance:

 df <- data.frame(x = c(1,2,3,4), y = c(2,3,4,7), z = c(5,6, 9, 1)) f <- formula('z~x*y') model.frame(f, df) 

exit:

 > df xyz 1 1 2 5 2 2 3 6 3 3 4 9 4 4 7 1 > f <- formula('z~x*y') > model.frame(f, df) zxy 1 5 1 2 2 6 2 3 3 9 3 4 4 1 4 7 

I hope to get:

  zxyx*y 1 5 1 2 2 2 6 2 3 6 3 9 3 4 12 4 1 4 7 28 

Is there a package that could achieve this functionality? (It would be ideal if I could get the resulting matrix as a sparse matrix , because the crossed columns will be very sparse)

+5
source share
1 answer

You can use model.matrix :

 > model.matrix(f, df) (Intercept) xyx:y 1 1 1 2 2 2 1 2 3 6 3 1 3 4 12 4 1 4 7 28 attr(,"assign") [1] 0 1 2 3 

If you want to save the result as a sparse matrix, you can use the Matrix package:

 > mat <- model.matrix(f, df) > library(Matrix) > Matrix(mat, sparse = TRUE) 4 x 4 sparse Matrix of class "dgCMatrix" (Intercept) xyx:y 1 1 1 2 2 2 1 2 3 6 3 1 3 4 12 4 1 4 7 28 
+4
source

Source: https://habr.com/ru/post/1204122/


All Articles