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
You could do something like this, but I'm almost loathe to suggest it because I feel it still avoids an underlying problem...
Note the "duplication" of the
Categorycolumns because they appear in both data frames and you apper not to want to join on them. I introduce aRowcolumn to each data frame so that the rows of the shorter df are bound to the corresponding rows of the longer.If you want
Categoryonly frommonthly_df, drop it fromquarterly_dfbeforejoining.