Home > Software engineering >  unused arguments when trying to create a matrix in R
unused arguments when trying to create a matrix in R

Time:05-18

I want create such matrix

dat <- matrix(
  "an_no" = c(14, 17), 
  "an_yes" = c(3, 1),
  row.names = c("TL-MCT-t", "ops"),
  stringsAsFactors = FALSE
)

but i get error unused arguments. What i did wrong and how perform correct matrix with such arguments? as.matrix didn't help.

Thanks for your help.

CodePudding user response:

You are using the arguments that you would use to build a data frame. If you want a matrix using this syntax you can do:

dat <- as.matrix(data.frame(
  an_no = c(14, 17), 
  an_yes = c(3, 1),
  row.names = c("TL-MCT-t", "ops")))

dat
#>          an_no an_yes
#> TL-MCT-t    14      3
#> ops         17      1

You don't need the stringsAsFactors = FALSE because none of your data elements are strings, and in any case, stringsAsFactors is FALSE by default unless you are using an old version of R. You also don't need quotation marks around an_no and an_yes because these are both legal variable names in R.

CodePudding user response:

The matrix function estructure is this:

matrix(data = NA, 
       nrow = 1,
       ncol = 1, 
       byrow = FALSE,
       dimnames = NULL)

Appears you're trying to create a data.frame

data.frame(row_names = c("TL-MCT-t", "ops"),
           an_no = c(14,17),
           an_yes = c(3,1)
)
  •  Tags:  
  • r
  • Related