4

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.

1
  • It is hard to answer the question without knowing why you need to create a data frame without column names because it is possible that R has a more powerful way of doing what you want without a for () loop. Don't assume that R does things the same way as other programming languages. Tell us what you are trying to accomplish, don't tell use how you want to accomplish it. Commented Feb 1, 2018 at 22:28

4 Answers 4

4

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
Sign up to request clarification or add additional context in comments.

4 Comments

I will add rows to the data frame not columns
rbind add rows, not columns
But can you also have the row something like this: row <- c(a=i, b=i, c=i)
Yes, you can. In this case you assign columns names inside the for loop. Answer changed according to this.
3
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.

Comments

3

If you know the type beforehand, you could do

df1 = data.frame(INTS = integer(0),
                 CHRS = character(),
                 NUMS = numeric(0))

for(i in 1:5){
    row = setNames(data.frame(i, letters[i], i + 0.5), names(df1))
    df1 = rbind(df1, row)
}

df1
#  INTS CHRS NUMS
#1    1    a  1.5
#2    2    b  2.5
#3    3    c  3.5
#4    4    d  4.5
#5    5    e  5.5

1 Comment

You still give names to the columns beforehand, I want to do that in the for loop.
2

Either NULL or convert a martix to a dataframe at the end of the loop:

df = data.frame(NULL)

for(i in 1:20){
  row = i
  df = rbind(df,row)
}
class(df)
> [1] "data.frame"

or

df = NULL

for(i in 1:20){
  row = i
  df = rbind(df,row)
}
class(df)
df = data.frame(df)
class(df)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.