mr3mo
mr3mo

Reputation: 145

What kind of object is data.frame(matrix(...)) in R

What does this code do?

xy <-data.frame(matrix(ncol = 2, nrow=100000, dimnames=list(NULL,c("X","Y"))))

Is there any other way of getting the same result without using the matrix function?

Upvotes: 0

Views: 34

Answers (1)

DanY
DanY

Reputation: 6073

The matrix is converted into a data.frame with column names X and Y.

You can always run the code yourself and call str(xy) to see the "structure" of the object.

> str(xy)
'data.frame':   100000 obs. of  2 variables:
 $ X: logi  NA NA NA NA NA NA ...
 $ Y: logi  NA NA NA NA NA NA ...

This tells you xy is a data.frame with 100,000 rows and 2 columns. The columns are named X and Y and the first few elements are logical NAs (they all are, but str only prints the first few).

If you want to skip the matrix part, just create the data.frame directly:

xy <- data.frame( X = rep(NA, 1e5), Y = rep(NA, 1e5))

Upvotes: 2

Related Questions