Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep row names when using rbind.fill in R

Tags:

r

rbind

plyr

Does anyone know how to keep rownames in the rbind.fill function.

library(plyr)

#creating data
a <- mtcars[ 1:5 , c("mpg","hp","gear") ]
b <- mtcars[ 6:10 , c("mpg","disp","gear") ]

#does not work because there are different colnames
rbind(a,b)

#works but eliminates the rownames
bound <- rbind.fill( a , b )

I am setting up a loop where objects will be connected using rbind.fill. Right now I am using the combine function like this:

namess <- c( rownames(a) , rownames(b) )
rownames(bound) <- namess

I thought that there might be a better way. Thanks!

like image 568
MatthewR Avatar asked May 07 '13 14:05

MatthewR


People also ask

How do you name rows in Rbind?

You can assign row names by passing your rbind output into a pipe to the function rownames() and mutate the row name as you like. Just make sure you mutate the correct row you intended.

What does Rbind fill do in R?

rbind. fill() function rbinds a list of data frames filling missing columns with NA. This function is located in package "plyr".

How do I Rbind a Dataframe in R?

To join two data frames (datasets) vertically, use the rbind function. The two data frames must have the same variables, but they do not have to be in the same order. If data frameA has variables that data frameB does not, then either: Delete the extra variables in data frameA or.

How does row names work in R?

Row names are currently allowed to be integer or character, but for backwards compatibility (with R <= 2.4. 0) row. names will always return a character vector. (Use attr(x, "row.


2 Answers

You could try to apply a custom function that performs rbind.fill and sets initial rownames automatically like that:

# List of sample data
ab.list <- list(a <- mtcars[1:5 , c("mpg","hp","gear")], 
                b <- mtcars[6:10 , c("mpg","disp","gear")])

# Apply custom function (rbind and rownames adjustment) to sample data
do.call(function(...) {

  tmp <- plyr::rbind.fill(...)
  rownames(tmp) <- sapply(ab.list, function(i) {
    rownames(i)
  })

  return(tmp)

}, ab.list)

                   mpg  hp gear  disp
Mazda RX4         21.0 110    4    NA
Mazda RX4 Wag     21.0 110    4    NA
Datsun 710        22.8  93    4    NA
Hornet 4 Drive    21.4 110    3    NA
Hornet Sportabout 18.7 175    3    NA
Valiant           18.1  NA    3 225.0
Duster 360        14.3  NA    3 360.0
Merc 240D         24.4  NA    4 146.7
Merc 230          22.8  NA    4 140.8
Merc 280          19.2  NA    4 167.6

Check out this previous post that deals with a similar problem.

like image 103
fdetsch Avatar answered Oct 05 '22 17:10

fdetsch


Since I posted this question five years ago - a new function became available. smartbind solves this issue.

library( gtools) 

#creating data
a <- mtcars[ 1:5 , c("mpg","hp","gear") ]
b <- mtcars[ 6:10 , c("mpg","disp","gear") ]

smartbind( a , b )
like image 39
MatthewR Avatar answered Oct 05 '22 19:10

MatthewR