Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create empty data.frame with column names

Tags:

dataframe

r

I am trying to create a empty data frame with two columns and unknown number of row. I would like to specify the names of the columns. I ran the following command

dat <- data.frame("id"=numeric(),"nobs"=numeric())  

I can test the result by running

> str(dat)
'data.frame':   0 obs. of  2 variables:
 $ id  : num 
 $ nobs: num   

But later on when I insert data into this data frame using rbind in the following command, the names of the columns are also changed

  for (i in id) {
    nobs = nrow(na.omit(read.csv(files_list[i])))
    dat = rbind(dat, c(i,nobs))
  } 

After for loop this is the value of dat

dat
  X3 X243
1  3  243

And str command shows the following

str(dat)

'data.frame':   1 obs. of  2 variables:
 $ X3  : num 3
 $ X243: num 243

Can any one tell why are the col names in data frame change

EDIT:

My lazy solution to fix the problem is to run the follwing commands after for loop that binds data to my data.frame

names(dat)[1] = "id"
names(dat)[2] = "nobs"
like image 786
Khurram Majeed Avatar asked Jul 22 '14 22:07

Khurram Majeed


People also ask

How do you create an empty DataFrame with certain columns?

Use pandas. DataFrame() to create an empty DataFrame with column names. Call pandas. DataFrame(columns = column_names) with column set to a list of strings column_names to create an empty DataFrame with column_names .

How do I create a DataFrame with specific column names in R?

How to Create a Data Frame. We can create a dataframe in R by passing the variable a,b,c,d into the data. frame() function. We can R create dataframe and name the columns with name() and simply specify the name of the variables.


1 Answers

Interestingly, the rbind.data.frame function throws away all values passed that have zero rows. It basically happens in this line

allargs <- allargs[nr > 0L]

so passing in a data.frame with no rows, is really like not passing it in nothing at all. Another good example why it's almost always a bad idea to try to build a data.frame row-by-row. Better to build vectors and then combine into a data.frame only when done.

like image 80
MrFlick Avatar answered Oct 05 '22 14:10

MrFlick