Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a data.frame with m columns and 2 rows

Tags:

r

I would like to create a data.frame in R with m (a variable) number of columns (for example 30), and 2 rows and fill all the values in the data.frame initially with 0's. It seems as though data.frame populates values based on rows rather that columns, any suggestions how I can do this? Thanks :)

like image 943
AEUser Avatar asked May 23 '11 22:05

AEUser


People also ask

Can we create a Dataframe having any number of rows and columns?

Yes it is possible to create any shape dataframe.

How do I specify the number of rows in a Dataframe in R?

To get number of rows in R Data Frame, call the nrow() function and pass the data frame as argument to this function. nrow() is a function in R base package.


1 Answers

Does m really need to be a data.frame() or will a matrix() suffice?

m <- matrix(0, ncol = 30, nrow = 2) 

You can wrap a data.frame() around that if you need to:

m <- data.frame(m) 

or all in one line: m <- data.frame(matrix(0, ncol = 30, nrow = 2))

like image 63
Chase Avatar answered Sep 28 '22 03:09

Chase