Search code examples
rmatrixcharacterelementdeterminants

Determinant of matrix with character elements in R


I have the following code:

x1 <- c("x", "y")
x2 <- c("z", "w")
X <-data.frame(x1,x2)
A=as.matrix(X)

The matrix A is the following:

      x1  x2 
[1,] "x" "z"
[2,] "y" "w"

How can I find the determinant of A? I want a result like xw-yz.

I tried det(A) but it does not work.

In general, I want to be able to find the determinant of any matrix whose elements are of character type.


Solution

  • You can use the giacR package:

    library(giacR)
    
    giac <- Giac$new()
    
    command <- "det([[x, y], [z, w]])"
    giac$execute(command)
    # [1] "x*w-y*z"
    
    giac$close()