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,130 points
170 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,060 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 in Data Analytics by Cherukuri
• 32,260 points
20 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
114 views
0 votes
1 answer

How to create excellent examples in R?

An excellent example must consist of the ...READ MORE

answered Apr 10, 2018 in Data Analytics by kappa3010
• 2,070 points

edited Apr 12, 2018 by kappa3010 43 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
138 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
• 45,910 points
69 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
180 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,060 points

edited Apr 12, 2018 by nirvana 4,051 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,060 points
57 views