Home > Software engineering >  How to create a R dataframe with two objects?
How to create a R dataframe with two objects?

Time:12-02

Suppose I would like to create a dataframe in R with two objects/ variables like this following two examples coming from the library(projpred).
The first example is:

projpred::df_gaussian
>     str(df_gaussian)
'data.frame':   100 obs. of  2 variables:
 $ x: num [1:100, 1:20] 0.274 2.245 -0.125 -0.544 -1.459 ...
 $ y: num  -1.275 1.843 0.459 0.564 1.873 ...

The second example is

projpred::df_binom
str(df_binom)
>     str(df_binom)
'data.frame':   100 obs. of  2 variables:
 $ x: num [1:100, 1:30] -0.619 1.094 -0.357 -2.469 0.567 ...
 $ y: int  0 1 1 0 1 0 0 0 1 1 ...

Clearly here the 'x' is a matrix of dimension 100 X 20 and 'y' is a vector/matrix of dimension 100 X 1. When I do the something like the following:

>     x<- matrix(rnorm(49,0,1),ncol=7,nrow=7)
> x
           [,1]       [,2]       [,3]       [,4]         [,5]       [,6]        [,7]
[1,]  0.7475965 0.25087585 -0.5454202  1.1080362  0.772668671  0.1541041 -0.18822798
[2,]  0.1156593 0.01525141 -1.7016563 -1.4725411 -1.103412611 -0.5244481 -1.35857198
[3,] -2.0756020 0.76945330  2.1603842 -0.7884491 -0.058697197  1.7486573 -0.22084569
[4,] -0.7190079 0.02477635 -0.1113622  0.2430216 -0.002865642  0.8650818  0.01232973
[5,] -0.9197059 0.88796240 -0.7654234 -1.3388553 -1.323093057 -0.6983747  1.20201014
[6,]  1.4298535 0.04451137  1.2678596  0.3640843 -0.046717376 -2.2444299  1.80306550
[7,] -0.3876859 0.62635356 -0.3490285 -0.9496578  1.150150174  0.4247856 -0.97021264
>     y<- rnorm(7,5,1)
> y
[1] 6.456016 4.984491 5.209759 7.183303 4.461657 5.005530 5.052837
> z<-data.frame(x,y)

I get something like below, which is not essentially what I want.


> z
          X1         X2         X3         X4           X5         X6          X7        y
1  0.7475965 0.25087585 -0.5454202  1.1080362  0.772668671  0.1541041 -0.18822798 6.456016
2  0.1156593 0.01525141 -1.7016563 -1.4725411 -1.103412611 -0.5244481 -1.35857198 4.984491
3 -2.0756020 0.76945330  2.1603842 -0.7884491 -0.058697197  1.7486573 -0.22084569 5.209759
4 -0.7190079 0.02477635 -0.1113622  0.2430216 -0.002865642  0.8650818  0.01232973 7.183303
5 -0.9197059 0.88796240 -0.7654234 -1.3388553 -1.323093057 -0.6983747  1.20201014 4.461657
6  1.4298535 0.04451137  1.2678596  0.3640843 -0.046717376 -2.2444299  1.80306550 5.005530
7 -0.3876859 0.62635356 -0.3490285 -0.9496578  1.150150174  0.4247856 -0.97021264 5.052837
>     str(z)
'data.frame':   7 obs. of  8 variables:
 $ X1: num  0.748 0.116 -2.076 -0.719 -0.92 ...
 $ X2: num  0.2509 0.0153 0.7695 0.0248 0.888 ...
 $ X3: num  -0.545 -1.702 2.16 -0.111 -0.765 ...
 $ X4: num  1.108 -1.473 -0.788 0.243 -1.339 ...
 $ X5: num  0.77267 -1.10341 -0.0587 -0.00287 -1.32309 ...
 $ X6: num  0.154 -0.524 1.749 0.865 -0.698 ...
 $ X7: num  -0.1882 -1.3586 -0.2208 0.0123 1.202 ...
 $ y : num  6.46 4.98 5.21 7.18 4.46 ...

CodePudding user response:

Wrap with I for asis - or else by calling the data.frame, the matrix will be converted to data.frame. It is documented in ?I

In function data.frame. Protecting an object by enclosing it in I() in a call to data.frame inhibits the conversion of character vectors to factors and the dropping of names, and ensures that matrices are inserted as single columns. I can also be used to protect objects which are to be added to a data frame, or converted to a data frame via as.data.frame.

z <- data.frame(x = I(x), y)
> str(z)
'data.frame':   7 obs. of  2 variables:
 $ x: 'AsIs' num [1:7, 1:7] -0.178 -1.37 -0.682 1.166 0.437 ...
 $ y: num  5.12 4.58 5.41 4.91 6.43 ...
> is.matrix(z$x)
[1] TRUE

If we need to change the class from "AsIs"

> class(z$x) <- c("matrix", "array")
> str(z)
'data.frame':   7 obs. of  2 variables:
 $ x: 'matrix' num [1:7, 1:7] -0.178 -1.37 -0.682 1.166 0.437 ...
 $ y: num  5.12 4.58 5.41 4.91 6.43 ...

Or another option is tibble

library(tibble)
z1 <- tibble(x, y)
str(z1)
tibble [7 × 2] (S3: tbl_df/tbl/data.frame)
 $ x: num [1:7, 1:7] -0.178 -1.37 -0.682 1.166 0.437 ...
 $ y: num [1:7] 5.12 4.58 5.41 4.91 6.43 ...
  • Related