Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create empty data frame with 200 rows and no columns

Tags:

dataframe

r

Here is how I can do that:

df <- data.frame(a=1:200)
df$a <- NULL
df

Result:

data frame with 0 columns and 200 rows

Can the same be achieved with only one command?

like image 366
January Avatar asked Jul 29 '15 13:07

January


People also ask

How do you create an empty DataFrame with certain number of rows?

To create an empty data frame with fixed number of rows but no columns, we can use data. frame function along with the matrix function. That means we need to create a matrix without any column using matrix and save it in a data frame using data.

How do you create an empty DataFrame with rows and columns in R?

One simple approach to creating an empty DataFrame in the R programming language is by using data. frame() method without any params. This creates an R DataFrame without rows and columns (0 rows and 0 columns).

How do you create an empty DataFrame of a certain size?

Create Empty Dataframe With Size You can create a dataframe with a specified size for both columns and rows. Use the range function to create a sequence of numbers and pass it to the index range or the columns range specify column and row sizes.


2 Answers

This can work (if the call to 2 functions is not considered 2 commands):

data.frame(matrix(, nrow=200, ncol=0))
#data frame with 0 columns and 200 rows

Edit: Another option is data.frame()[1:200, ]:

data.frame()[1:200, ]
# data frame with 0 columns and 200 rows
like image 118
Cath Avatar answered Sep 20 '22 23:09

Cath


What about this?

data.frame(row.names = 1:200)
like image 22
Paul Avatar answered Sep 19 '22 23:09

Paul