Back

Explore Courses Blog Tutorials Interview Questions
+4 votes
7 views
in R Programming by (2.6k points)
edited by

I want a way to create a empty data.frame in R, I just want to point out data types for each column and name it without any row created.

df <- data.frame(Date=as.Date("29/05/2019", format="%d/%m/%Y"), 
                 File="", User="", stringsAsFactors=FALSE)
df <- df[-1,]

currently I am using this command, which is doing  my work but also creating a row which is waste to me.

Suggest me a more efficient way to do it.

2 Answers

+3 votes
by
edited by

To create a data frame with empty rows, just initialize it with empty vectors as follows:

dataframe <- data.frame(Date=as.Date(character()),

                 File=character(), 

                 User=character(), 

                 stringsAsFactors=FALSE) 

str(dataframe)

Output:

'data.frame':  0 obs. of  3 variables:

 $ Date: 'Date' num(0) 

 $ File: chr

 $ User: chr

If you want to learn more about R programming watch this tutorial on Introduction to Data Science with R

+4 votes
by (32.3k points)
edited by

To get empty data.frame you can just create data.frame by using only 0-length variables and it'll give you empty data.frame.

dataframe <- data.frame(Date=as.Date(character(0)),

                 File=character(0), 

                 User=character(0), 

                 stringsAsFactors=FALSE) 

str(dataframe)

Related questions

+3 votes
1 answer
asked May 29, 2019 in R Programming by Anvi (10.2k points)
+1 vote
1 answer
asked May 23, 2019 in R Programming by Nigam (4k points)

Browse Categories

...