+5 votes
2 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

+4 votes
by (25.3k points)
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 

+5 votes
by (31.4k points)

In order to get empty data.frame you can just create a data.frame by using only 0-length variables and it'll give you an 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 (4.1k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...