How to create an empty dataframe

0 votes

I'm trying to specify the data types for each column and name them. So basically I'm trying to create a data frame without any rows.

I have tried doing something like this:

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

The above code creates a data frame with a single row containing all the data types and the column names, but this also creates a useless row which needs ti be removed.

Is there any better way to implement this?

Apr 13, 2018 in Data Analytics by DataKing99
• 8,250 points
1,388 views

1 answer to this question.

0 votes

Initialize with empty vectors:

data <- data.frame(Date=as.Date(character()),
                 File=character(), 
                 User=character(), 
                 stringsAsFactors=FALSE) 

Below is another example with different column types :

data <- data.frame(Doubles=double(),
                 Ints=integer(),
                 Factors=factor(),
                 Logicals=logical(),
                 Characters=character(),
                 stringsAsFactors=FALSE)

str(data)
str(data)
'data.frame':   0 obs. of  5 variables:
 $ Doubles   : num 
 $ Ints      : int 
 $ Factors   : Factor w/ 0 levels: 
 $ Logicals  : logi 
 $ Characters: chr 
answered Apr 13, 2018 by nirvana
• 3,130 points

Related Questions In Data Analytics

0 votes
1 answer

How to combine 2 fields in a dataframe to create a new field?

Use paste command or append function to ...READ MORE

answered Oct 31, 2019 in Data Analytics by Cherukuri
• 33,030 points
592 views
+2 votes
2 answers

How to create our own dataframe in R?

Data frames are tabular data objects. Unlike ...READ MORE

answered Oct 29, 2020 in Data Analytics by anonymous
1,365 views
0 votes
1 answer

How to create an Array in R?

Hi@akhtar, The array function takes a dim attribute ...READ MORE

answered Oct 29, 2020 in Data Analytics by MD
• 95,460 points
644 views
0 votes
1 answer
+1 vote
1 answer

How to convert a list of vectors with various length into a Data.Frame?

We can easily use this command as.data.frame(lapply(d1, "length< ...READ MORE

answered Apr 4, 2018 in Data Analytics by DeepCoder786
• 1,720 points
1,661 views
0 votes
2 answers

In data frame how to spilt strings into values?

You can do this using dplyr and ...READ MORE

answered Dec 5, 2018 in Data Analytics by Kalgi
• 52,350 points
1,100 views
0 votes
1 answer
0 votes
1 answer

How to convert a text mining termDocumentMatrix into excel or csv in R?

By assuming that all the values are ...READ MORE

answered Apr 5, 2018 in Data Analytics by DeepCoder786
• 1,720 points
1,921 views
0 votes
1 answer

How to convert a list to data frame in R?

Let's assume your list of lists is ...READ MORE

answered Apr 12, 2018 in Data Analytics by nirvana
• 3,130 points

edited Apr 12, 2018 by nirvana 22,104 views
0 votes
1 answer

Is there any way to check for missing packages and install them in R?

There are 2 options: Either you can use ...READ MORE

answered Apr 17, 2018 in Data Analytics by nirvana
• 3,130 points
875 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP