Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (5.3k points)
edited by

I am just starting with R and encountered a strange behavior: when inserting the first row in an empty data frame, the original column names get lost.

example:

a<-data.frame(one = numeric(0), two = numeric(0))

a

#[1] one two

#<0 rows> (or 0-length row.names)

names(a)

#[1] "one" "two"

a<-rbind(a, c(5,6))

a

#  X5 X6

#1  5  6

names(a)

#[1] "X5" "X6"

As you can see, the column names one and two were replaced by X5 and X6.

Could somebody please tell me why this happens and is there a right way to do this without losing column names?

A shotgun solution would be to save the names in an auxiliary vector and then add them back when finished working on the data frame.

Thanks

Context:

I created a function which gathers some data and adds them as a new row to a data frame received as a parameter. I create the data frame, iterate through my data sources, passing the data.frame to each function call to be filled up with its results.

1 Answer

0 votes
by
edited by

According to R Documentation:

For ‘cbind’ (‘rbind’), vectors of zero length (including ‘NULL’) are ignored unless the result would have zero rows (columns), for S compatibility. (Zero-extent matrices do not occur in S3 and are not ignored in R.)

When you use rbind function, the matrix “a” is ignored but not totally ignored, it seems because as it is a data frame the rbind function is called as rbind.data.frame :

To keep the column names, you can do the following:

> a<-data.frame(one = numeric(0), two = numeric(0))

> a

[1] one two

<0 rows> (or 0-length row.names)

> names(a)

[1] "one" "two"

a[nrow(a)+1,] <- c(5,6)

> a

  one two

1   5   6

If you want to explore more in R programming then watch this R programming tutorial for beginners:

Browse Categories

...