Reputation: 2391
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"
Upvotes: 16
Views: 33672
Reputation: 31
dat = data.frame(col1=numeric(), col2=numeric())
...loop
dat[, dim(dat)[1] + 1] = c(324, 234)
This keeps the column names
Upvotes: 2
Reputation: 490
You should try specify your column names inside the rbind():
dat = rbind(dat, data.frame("id" = i, "nobs" = nobs))
Upvotes: 1
Reputation: 1
I would change how you're appending the data to the data frame. Since rbind seems to remove the column names, just replace the indexed location.
dat <- data.frame("id"=numeric(),"nobs"=numeric())
for (i in id) {
dat[i,] <- nrow(na.omit(read.csv(files_list[i])))
}
FYI, Default data frame creation converts all strings to factors, not an issue here, since all your data formats are numeric. But if you had a character(), you might want to turn off the default stringsAsFactors=FALSE, to append character lists.
Upvotes: 0
Reputation: 206167
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.
Upvotes: 10