Sam
Sam

Reputation: 548

R cbind two dataframes of different length throwing error invalid 'nrow' value (< 0)

I am trying to cbind two dataframes of different lengths.

Category = c("New", "New","New","New","Old","Old","Old","Old",
"Expired", "Expired", "Expired", "Expired")
Monthly_Dates= c ('Jan-2014', 'Feb-2014', 'Mar-2014', 'Apr-2014',
'May-2014', 'Jun-2014', 'Jul-2014', 'Aug-2014', 'Sep-2014','Oct-2014','Nov-2014','Dec-2014')
Monthly_Vals =   c(4,7,6,7,8,12,10,11,10,16,12,120)

monthly_df <- data.frame(Category, Monthly_Dates,Monthly_Vals) 
Category = c("New","Old", "Expired",  "Expired")
Quarterly_Date= c ( 'Mar-2014', 'Jun-2014', 'Sep-2014', 'Dec-2014')
Quarterly_Vals =   c(4,7,6,7)

quarterly_df <- data.frame(Category, Quarterly_Date,Quarterly_Vals) 
monthly_df = cbind(monthly_df, matrix(data = NA, ncol = ncol(monthly_df), 
nrow = nrow(quarterly_df) - nrow(monthly_df),dimnames = list(NULL, names(monthly_df))));
Combined_dfs <- data.frame(monthly_df, quarterly_df)
View (Combined_dfs)

When i try to cbind monthly and quarterly values it throws error, what am i doing wrong?

Error

Error in matrix(data = NA, ncol = ncol(monthly_df), nrow = nrow(quarterly_df) -  : 
  invalid 'nrow' value (< 0)

Expected Outcome

   Category Monthly_Dates Monthly_Vals   Category  Quarterly_Date  Quarterly_Vals
1       New      Jan-2014            4   New       Mar-2014        4
2       New      Feb-2014            7   Old       Jun-2014        7
3       New      Mar-2014            6   Expired   Sep-2014        6
4       New      Apr-2014            7   Expired   Dec-2014        7
5       Old      May-2014            8
6       Old      Jun-2014           12
7       Old      Jul-2014           10
8       Old      Aug-2014           11
9   Expired      Sep-2014           10
10  Expired      Oct-2014           16
11  Expired      Nov-2014           12
12  Expired      Dec-2014          120

Upvotes: -1

Views: 64

Answers (2)

Limey
Limey

Reputation: 12586

You could do something like this, but I'm almost loathe to suggest it because I feel it still avoids an underlying problem...

library(tidyverse)

monthly_df <- monthly_df %>% mutate(Row = row_number())
quarterly_df <- quarterly_df %>% mutate(Row = row_number())

monthly_df %>% full_join(quarterly_df, by = "Row")
   Category.x Monthly_Dates Monthly_Vals Row Category.y Quarterly_Date Quarterly_Vals
1         New      Jan-2014            4   1        New       Mar-2014              4
2         New      Feb-2014            7   2        Old       Jun-2014              7
3         New      Mar-2014            6   3    Expired       Sep-2014              6
4         New      Apr-2014            7   4    Expired       Dec-2014              7
5         Old      May-2014            8   5       <NA>           <NA>             NA
6         Old      Jun-2014           12   6       <NA>           <NA>             NA
7         Old      Jul-2014           10   7       <NA>           <NA>             NA
8         Old      Aug-2014           11   8       <NA>           <NA>             NA
9     Expired      Sep-2014           10   9       <NA>           <NA>             NA
10    Expired      Oct-2014           16  10       <NA>           <NA>             NA
11    Expired      Nov-2014           12  11       <NA>           <NA>             NA
12    Expired      Dec-2014          120  12       <NA>           <NA>             NA

Note the "duplication" of the Category columns because they appear in both data frames and you apper not to want to join on them. I introduce a Row column to each data frame so that the rows of the shorter df are bound to the corresponding rows of the longer.

If you want Category only from monthly_df, drop it from quarterly_df before joining.

Upvotes: 2

Friede
Friede

Reputation: 7979

I think you are looking for something completely different. Probably a basci appraoch involving merge (and friends). However, here an answer to your question.

quarterly_df = rbind(quarterly_df, 
                     matrix(data = "", ncol = ncol(quarterly_df), nrow = nrow(monthly_df) - nrow(quarterly_df), 
                            dimnames = list(NULL, names(quarterly_df))))
combined = data.frame(monthly_df, quarterly_df)
combined$Category.1 = NULL

gives

   Category Monthly_Dates Monthly_Vals Quarterly_Date Quarterly_Vals
1       New      Jan-2014            4       Mar-2014              4
2       New      Feb-2014            7       Jun-2014              7
3       New      Mar-2014            6       Sep-2014              6
4       New      Apr-2014            7       Dec-2014              7
5       Old      May-2014            8                              
6       Old      Jun-2014           12                              
7       Old      Jul-2014           10                              
8       Old      Aug-2014           11                              
9   Expired      Sep-2014           10                              
10  Expired      Oct-2014           16                              
11  Expired      Nov-2014           12                              
12  Expired      Dec-2014          120    

Upvotes: 1

Related Questions