Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create an empty data.frame without specifying column names

Tags:

dataframe

r

I would like to have something like this:

#initialize a empty data frame without saying what the column names are    
dataFrame = data.frame()

for(1:20){

Create a row 

dataFrame = rbind(dataFrame, row)

}

The rows created in the for loop will have all the same column names. So is it possible to not initialize the column names?

Update:

Thank you all for the help.
But I wasn't clear with my answer, sorry.
I would like to have the names of the columns, stated in the for loop.

I now get the following message:
Error in match.names(clabs, nmi) : names do not match previous names
And what I can see is that the spaces in the column names get changed to dots. Is there a way so the names don't change or change beforehand.

like image 235
Cing Avatar asked Dec 14 '22 18:12

Cing


2 Answers

Inizialize it like this:

dataFrame<-NULL
db2<-data.frame(a=c(1,1,0,1,0,0)) 
rbind(dataFrame,db2)
  a
1 1
2 1
3 0
4 1
5 0
6 0

From your question-code (changed a little bit to make it running):

for(i in 1:5)
{
   row<-c(a=i,b=i,c=i)
   dataFrame = rbind(dataFrame, row)
}

The output

DataFrame
    a b c
row 1 1 1
row 2 2 2
row 3 3 3
row 4 4 4
row 5 5 5
like image 195
Terru_theTerror Avatar answered Dec 18 '22 00:12

Terru_theTerror


for(i in 1:20){

# create a row 

if(i == 1) dataFrame <- t(row)
else dataFrame <- rbind(dataFrame, row)

}

Really though, it would be better to initialize a vector for each column, populate the vectors with the loop, then create a dataframe after the loop.

like image 45
IceCreamToucan Avatar answered Dec 18 '22 00:12

IceCreamToucan