jvel
jvel

Reputation: 21

How to create an empty dataframe with specified columns AND number of rows in R?

I have two dataframes that I want to be able to rbind together. They have the similar information in them, but not in the same order and not with the same column names (and this is a mix of strings, integers, and real numbers, so matrices will not work).

What I then need to do is convert one of the dataframes (we'll call it new_df) into the same structure as the other dataframe (we'll call it old_df).

I want to create an empty dataframe with the column structure of old_df AND the same number of rows as new_df.

I know I can create the first part of that with empty_df <- old_df[0,], but how can I specify the number of rows?

I know the number of rows I want to end up with, so I'd like to specify that. I cannot find this anywhere.

What I want is something like this (if this worked):

empty_df <- old_df[rep(0,nrow(new_df)),]

I tried:

empty_df <- old_df[rep(0,nrow(new_df)),]

empty_df <- old_df[0,]

empty_df$ID <- new_df$ids

Upvotes: 1

Views: 1182

Answers (1)

Rui Barradas
Rui Barradas

Reputation: 76651

If I understand the question correctly, the following hack will do what the OP wants. It creates the number of rows by setting the row.names attribute directly. And if a dataframe has row names, it must have the corresponding rows.

empty_df <- old_df[0, ]
attr(empty_df, 'row.names') <- 1:nrow(new_df)

str(empty_df)
#'data.frame':  300 obs. of  5 variables:
# $ Sepal.Length: num 
# $ Sepal.Width : num 
# $ Petal.Length: num 
# $ Petal.Width : num 
# $ Species     : Factor w/ 3 levels "setosa","versicolor",..:

The dataframe empty_df now has 300 rows.

Data creation code.

The test data creation code uses the built-in dataset iris.

set.seed(1234)

old_df <- iris
new_df <- rbind(iris, iris)
new_df <- new_df[, sample(ncol(new_df))]

Upvotes: 2

Related Questions