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,210 points
500 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,120 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
• 32,490 points
67 views
+2 votes
1 answer

How to create our own dataframe in R?

Hi@akhtar, Let's say you want to create one ...READ MORE

answered Apr 29 in Data Analytics by MD
• 57,060 points
178 views
0 votes
1 answer
0 votes
1 answer

How to create a list of Data frames?

Basically all we have to do is ...READ MORE

answered Apr 9, 2018 in Data Analytics by DeepCoder786
• 1,720 points
274 views
+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
363 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 4, 2018 in Data Analytics by Kalgi
• 51,950 points
160 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
424 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,120 points

edited Apr 12, 2018 by nirvana 13,692 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,120 points
139 views