Adrian
Adrian

Reputation: 9793

How to extract certain outputs from a non-S4 object?

require(datasets)
require(splines)
obj <- bs(women$height, df = 5)
obj
                 1           2           3            4           5
 [1,] 0.000000e+00 0.000000000 0.000000000 0.000000e+00 0.000000000
 [2,] 4.534439e-01 0.059857872 0.001639942 0.000000e+00 0.000000000
 [3,] 5.969388e-01 0.203352770 0.013119534 0.000000e+00 0.000000000
 [4,] 5.338010e-01 0.376366618 0.044278426 0.000000e+00 0.000000000
 [5,] 3.673469e-01 0.524781341 0.104956268 0.000000e+00 0.000000000
 [6,] 2.001640e-01 0.595025510 0.204719388 9.110787e-05 0.000000000
 [7,] 9.110787e-02 0.566326531 0.336734694 5.830904e-03 0.000000000
 [8,] 3.125000e-02 0.468750000 0.468750000 3.125000e-02 0.000000000
 [9,] 5.830904e-03 0.336734694 0.566326531 9.110787e-02 0.000000000
[10,] 9.110787e-05 0.204719388 0.595025510 2.001640e-01 0.000000000
[11,] 0.000000e+00 0.104956268 0.524781341 3.673469e-01 0.002915452
[12,] 0.000000e+00 0.044278426 0.376366618 5.338010e-01 0.045553936
[13,] 0.000000e+00 0.013119534 0.203352770 5.969388e-01 0.186588921
[14,] 0.000000e+00 0.001639942 0.059857872 4.534439e-01 0.485058309
[15,] 0.000000e+00 0.000000000 0.000000000 0.000000e+00 1.000000000
attr(,"degree")
[1] 3
attr(,"knots")
33.33333% 66.66667% 
 62.66667  67.33333 
attr(,"Boundary.knots")
[1] 58 72
attr(,"intercept")
[1] FALSE
attr(,"class")
[1] "bs"     "basis"  "matrix"

I want to extract the 15 x 5 matrix of values from obj, which is a bs object. I've tried using the @ operator but that didn't seem to do the trick.

Upvotes: 1

Views: 32

Answers (1)

akrun
akrun

Reputation: 887028

It is just a matrix with some attributes

attributes(obj)$class

So the 'obj' should have all the properties of the matrix with some extra. An easy way to get rid of the attributes, is just convert to data.frame and then to matrix again

 as.matrix(as.data.frame(obj))
                 1           2           3            4           5
 [1,] 0.000000e+00 0.000000000 0.000000000 0.000000e+00 0.000000000
 [2,] 4.534439e-01 0.059857872 0.001639942 0.000000e+00 0.000000000
 [3,] 5.969388e-01 0.203352770 0.013119534 0.000000e+00 0.000000000
 [4,] 5.338010e-01 0.376366618 0.044278426 0.000000e+00 0.000000000
 [5,] 3.673469e-01 0.524781341 0.104956268 0.000000e+00 0.000000000
 [6,] 2.001640e-01 0.595025510 0.204719388 9.110787e-05 0.000000000
 [7,] 9.110787e-02 0.566326531 0.336734694 5.830904e-03 0.000000000
 [8,] 3.125000e-02 0.468750000 0.468750000 3.125000e-02 0.000000000
 [9,] 5.830904e-03 0.336734694 0.566326531 9.110787e-02 0.000000000
[10,] 9.110787e-05 0.204719388 0.595025510 2.001640e-01 0.000000000
[11,] 0.000000e+00 0.104956268 0.524781341 3.673469e-01 0.002915452
[12,] 0.000000e+00 0.044278426 0.376366618 5.338010e-01 0.045553936
[13,] 0.000000e+00 0.013119534 0.203352770 5.969388e-01 0.186588921
[14,] 0.000000e+00 0.001639942 0.059857872 4.534439e-01 0.485058309
[15,] 0.000000e+00 0.000000000 0.000000000 0.000000e+00 1.000000000

Or another option is to assign those additional attributes to NULL. If this is done on the same object, we will lose those attributes

attributes(obj)[setdiff(names(attributes(obj)), c("dim", "dimnames"))] <- NULL
> obj
                 1           2           3            4           5
 [1,] 0.000000e+00 0.000000000 0.000000000 0.000000e+00 0.000000000
 [2,] 4.534439e-01 0.059857872 0.001639942 0.000000e+00 0.000000000
 [3,] 5.969388e-01 0.203352770 0.013119534 0.000000e+00 0.000000000
 [4,] 5.338010e-01 0.376366618 0.044278426 0.000000e+00 0.000000000
 [5,] 3.673469e-01 0.524781341 0.104956268 0.000000e+00 0.000000000
 [6,] 2.001640e-01 0.595025510 0.204719388 9.110787e-05 0.000000000
 [7,] 9.110787e-02 0.566326531 0.336734694 5.830904e-03 0.000000000
 [8,] 3.125000e-02 0.468750000 0.468750000 3.125000e-02 0.000000000
 [9,] 5.830904e-03 0.336734694 0.566326531 9.110787e-02 0.000000000
[10,] 9.110787e-05 0.204719388 0.595025510 2.001640e-01 0.000000000
[11,] 0.000000e+00 0.104956268 0.524781341 3.673469e-01 0.002915452
[12,] 0.000000e+00 0.044278426 0.376366618 5.338010e-01 0.045553936
[13,] 0.000000e+00 0.013119534 0.203352770 5.969388e-01 0.186588921
[14,] 0.000000e+00 0.001639942 0.059857872 4.534439e-01 0.485058309
[15,] 0.000000e+00 0.000000000 0.000000000 0.000000e+00 1.000000000
> str(obj)
 num [1:15, 1:5] 0 0.453 0.597 0.534 0.367 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:5] "1" "2" "3" "4" ...

Upvotes: 1

Related Questions