I want to create a matrix with rows and columns


#1

I wanted to write a command to create a matrix named WordMatrix with 2 rows and 3 columns. This is what I did but it did not run.

MyMatrix = matrix (c(word1,word2,word3,word4,word5,word6)
nrow=2,,
ncol=3
byrow= TRUE)

MyMatrix #print the matrix
[,1] [,2] [,3]
[1,] word1 word2 word3
[2,] word4 word5 word6


#3

Be sure to post your question as a reprex (FAQ: What's a reproducible example (`reprex`) and how do I do one?).

When you do, you'll note the code were you setup the matrix produces an error message:


MyMatrix = 
  matrix (
    c(word1,word2,word3,word4,word5,word6)
    nrow=2,,
    ncol=3
    byrow= TRUE)
#> Error: <text>:5:5: unexpected symbol
#> 4:     c(word1,word2,word3,word4,word5,word6)
#> 5:     nrow
#>        ^

Created on 2018-04-23 by the reprex package (v0.2.0).

  1. Note the double commas after nrow=2, there should be only one there. and one comma after ncol=3
  2. Also character-strings in R need to be surrounded by quotation marks. So your vector should be something like c("word1","word2","word3","word4","word5","word6"), unless you assigned a string to word1, word2,... elsewhere.