31  cbind and rbind (to combine dataframes)

#-----------------------------------------------------------------------------
# cbind and rbind functions
#
# cbind combines (i.e. "binds") columns from multiple dataframes, matrices and vectors.
# rbind combines (i.e. "binds") columns from multiple dataframes, matrices and vectors.
#
# For both cbind and rbind
#   - If all items being "bound" are vectors and matrices then the result is a matrix.
#   - If any of the items being "bound" is a dataframe, the result is a dataframe.
#-----------------------------------------------------------------------------

#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
# Use cbind to vectors into a matrix
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

ones = c(1,2,3,4,5)
tens = c(10,20,30,40,50)
hundreds = c(100,200,300,400,500)

nums = cbind(ones, tens, hundreds)   # a matrix with 3 columns
class(nums)   # "matrix" "array"
[1] "matrix" "array" 
# add another column to the matrix
nums = cbind(nums, thousands = c(1000,2000,3000,4000,5000))
nums
     ones tens hundreds thousands
[1,]    1   10      100      1000
[2,]    2   20      200      2000
[3,]    3   30      300      3000
[4,]    4   40      400      4000
[5,]    5   50      500      5000
# another matrix
mat = matrix( c(11,22,33,44,55,66,77,88,99,0), nrow=5, ncol=2)
mat
     [,1] [,2]
[1,]   11   66
[2,]   22   77
[3,]   33   88
[4,]   44   99
[5,]   55    0
cbind(nums, mat)
     ones tens hundreds thousands      
[1,]    1   10      100      1000 11 66
[2,]    2   20      200      2000 22 77
[3,]    3   30      300      3000 33 88
[4,]    4   40      400      4000 44 99
[5,]    5   50      500      5000 55  0
mat = matrix(doubles=c(11,22,33,44,55), tripples=c(111,222,333,444,555))
Error in matrix(doubles = c(11, 22, 33, 44, 55), tripples = c(111, 222, : unused arguments (doubles = c(11, 22, 33, 44, 55), tripples = c(111, 222, 333, 444, 555))
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
# Use cbind to combine columns from different
# dataframes and vectors into one dataframe
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

# A few rows
df1 = data.frame ( students = c("alice", "bob", "carla"),
                   
                   #                   year     = factor( c("junior", "freshman", "junior"),
                   #                                levels = c("freshman", "sophomore", "junior", "senior"),
                   #                                ordered = TRUE),
                   
                   test1 =    c(71, 81, 91),
                   
                   stringsAsFactors = FALSE)
df1
  students test1
1    alice    71
2      bob    81
3    carla    91
# two more tests
df2 = data.frame ( test2 = c(72, 82, 92),
                   test3 = c(73, 83, 93),
                   test4 = c(74, 84, 94) )
df2
  test2 test3 test4
1    72    73    74
2    82    83    84
3    92    93    94
# yet another test
df3 = data.frame ( test4 = c(74, 84, 94) )
df3
  test4
1    74
2    84
3    94
# use cbind to create a new data.frame with all the columns
allTests = cbind(df1, df2, df3)   
allTests
  students test1 test2 test3 test4 test4
1    alice    71    72    73    74    74
2      bob    81    82    83    84    84
3    carla    91    92    93    94    94
# cbind can combine vectors and dataframes

cbind( satScore = c(1200, 1150, 1400), allTests, test5=c(75,85,95) )
  satScore students test1 test2 test3 test4 test4 test5
1     1200    alice    71    72    73    74    74    75
2     1150      bob    81    82    83    84    84    85
3     1400    carla    91    92    93    94    94    95
# You can also use cbind to combine a vectors
# The result is a matrix
mat = cbind (x = c(1,2,3), y=c(100,200,300), z=c(111, 222, 333))
mat
     x   y   z
[1,] 1 100 111
[2,] 2 200 222
[3,] 3 300 333
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
# Use rbind to combine rows from different dataframes
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

# A few more rows
df3 = data.frame ( students = c("david", "ed", "fran"),
                   
                   #                   year     = factor( c("junior", "freshman", "junior"),
                   #                                      levels = c("freshman", "sophomore", "junior", "senior"),
                   #                                      ordered = TRUE),
                   
                   test1 =    c(75, 85, 95),
                   
                   stringsAsFactors = FALSE)
df2 
  test2 test3 test4
1    72    73    74
2    82    83    84
3    92    93    94
# Use "rbind" to combine the rows from df1 with the rows from df2 into a
# single data.frame.
#
# The "r" in "rbind" stands for "row" since we are "binding the rows together")

allRows = rbind(df1, df2)
Error in rbind(deparse.level, ...): numbers of columns of arguments do not match
allRows
Error in eval(expr, envir, enclos): object 'allRows' not found
x = rbind(allRows, c("a","b","c"))
Error in eval(expr, envir, enclos): object 'allRows' not found
str(x)
Error in eval(expr, envir, enclos): object 'x' not found
rbind(ones=c(1,2,3), tens=c(10,20,30) )
     [,1] [,2] [,3]
ones    1    2    3
tens   10   20   30