unexpected error in code '>'

following exact code replication, I get this error. Why?

> vector1 <- c(4,2,1)
> vector2 <- c(22,34,76,98,65)
> column.names <- c("COL1", "COL2", "COL3")
> row.names <- c("ROW1", "ROW2", "ROW3")
> matrix.names <- c("Matrix1", "Matrix2")
> result <- array(c(vector1, vector2),dim = c(4,2,1),dimnames = list(row.names,column.names,matrix.names))print(result)

this results in the following:

Error: unexpected symbol in " result <- array(c(vector1, vector2),dim = c(4,2,1),dimnames = list(row.names,column.names,matrix.names))print"

I can't find a bug. But can you?

HI @IsaanBoy,
Welcome to the RStudio Community Forum.

The length of the 'dimension_name' strings have to match the array dimensions you are defining. In addition, you had an erroneous print() on the end of the last line.

vector1 <- c(4,2,1)
vector2 <- c(22,34,76,98,65)
column.names <- c("COL1", "COL2")
row.names <- c("ROW1", "ROW2", "ROW3", "ROW4")
matrix.names <- c("Matrix1")
result <- array(c(vector1, vector2),
                dim = c(4,2,1),
                dimnames = list(row.names, column.names, matrix.names))
result
#> , , Matrix1
#> 
#>      COL1 COL2
#> ROW1    4   34
#> ROW2    2   76
#> ROW3    1   98
#> ROW4   22   65

Created on 2021-08-01 by the reprex package (v2.0.0)

how u get so smart?
:innocent:

As I adjust col and rows I notice that nothing changes. THis is not right!

Please be specific. Provide an example of the changed code.

vector1 <- c(4,2,1,3,5,7)
vector2 <- c(22,34,76,88,98,65,76,44,33)
column.names <- c("COL1", "COL2", "COL3" )
row.names <- c("ROW1", "ROW2", "ROW3" , "ROW4")
matrix.names <- c(
"Matrix1", "Matrix2")
result <- array(c(vector1, vector2),dim = c(4,3,1)
dimnames = list(row.names, column.names, matrix.names))
print(result)

Or changing dim :result <- array(c(vector1, vector2),dim = c(3,4,1)
doesn't do anything. I thought it would rearrange the order from colum length to rows; i thought that the order of numbers might change--but then I think there is another Boolean statement that does that but where to put it? Essentially both matrixes are the same. How to achieve one which emphasizes rows in listing numbers and the other emphasizing columns--how to extend columns to four? I thought I did that but it doesn't work--same with rows. for that matter, the input of more numbers is not reflected in the print result? Why?
vector1 <- c(4,2,1,3,5,7)
vector2 <- c(22,34,76,88,98,65,76,44,33)
column.names <- c("COL1", "COL2", "COL3" )
row.names <- c("ROW1", "ROW2", "ROW3" , "ROW4")
matrix.names <- c(
"Matrix1", "Matrix2")
result <- array(c(vector1, vector2),dim = c(3,4,1)
dimnames = list(row.names, column.names, matrix.names))
print(result)

It seems you may be running code, and checking resuts and perhaps not noticing errors being printed to your console ?
You have two kinds of issues, one is a simply R syntax issue, of missing comma, the other might be considered an issue of the arithmetic of the array creation.
First the easy one. I see in your latest examples where you use array() there is no comma between the dim = and the dimmnames = , therefore R must try to understand dimnames = as though it were part of dim= and not something seperate. Therefore this will fix some of your issue.

The second problem you seem to have is the arithmetic of your dimensions being apparently confused.
Lets consider the first of your two latest examples.

vector1 <- c(4,2,1,3,5,7)
vector2 <- c(22,34,76,88,98,65,76,44,33)
column.names <- c("COL1", "COL2", "COL3" )
row.names <- c("ROW1", "ROW2", "ROW3" , "ROW4")
matrix.names <- c(
  "Matrix1", "Matrix2")
result <- array(c(vector1, vector2),dim = c(4,3,1),
                dimnames = list(row.names, column.names, matrix.names))
print(result)

See that I added the required comma.
Now however we have a descriptive error.

length of 'dimnames' [3] not equal to array extent

lets think about what your commands amount to

length(c(vector1,vector2))
[1] 15

So there are 15 numbers to place inside an array of whatever dimensions...

column.names <- c("COL1", "COL2", "COL3" )
row.names <- c("ROW1", "ROW2", "ROW3" , "ROW4")

so we are going to arrange 15 numbers in some multiple of a 3x4 i.e. 12 number containing grid. ...
Now, this might be possible although confusing, given that you can technically mismatch the array content with the array dimensions, and have your content truncated or auto replicated to fill the space.
simple examples:

> array(c(1,2),dim=c(2,2))
     [,1] [,2]
[1,]    1    1
[2,]    2    2
> array(1:5,dim=c(2,2))
     [,1] [,2]
[1,]    1    3
[2,]    2    4

ok, but its a bit confusing, and for a handcrafted example like you have, seems possibly just a mistake. for example why even define vector1 and vector2 in two uneven sized parts, what meaning does that have ? anyway.
The critical can not compute issue is seen when you get to the final array() call and the dim does not match up with you dimnames. i.e. drop your dimnames param and the code will work. but if you want your dimnames there, which include Matrix1 and Matrix2 as names, then you must reflect that dimension length within dim with c(3,4,2), rather than the c(3,4,1) you have provided.

Hope this helps.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.