Search code examples
rmatrixprobabilitybindata

How can I subtract the elements I need from the output of a function?


I am new to R and I wanted to generate a matrix of joint probabilities.

Using this function:

> simul.commonprob(margprob=c(0.1,0.25,0.2), corr=0, method="integrate", n1=10^5, n2=10)

I got the following results:

0 0.1 0.1 : done
0 0.1 0.25 : done
0 0.1 0.2 : done
0 0.25 0.25 : done
0 0.25 0.2 : done
0 0.2 0.2 : done
, , 0

       0.1   0.25  0.2
0.1  0.010 0.0250 0.02
0.25 0.025 0.0625 0.05
0.2  0.020 0.0500 0.04

I want to substract this matrix so I can use it later as an input to another function, in other words I want this result:

0.1   0.25  0.2
0.1  0.010 0.0250 0.02
0.25 0.025 0.0625 0.05
0.2  0.020 0.0500 0.04

How can I get it?


Solution

  • The matrix that you get is the returned object from the function that you call. You can store it in an object that you define like this:

    myOutput <- simul.commonprob(margprob=c(0.1,0.25,0.2), corr=0, method="integrate", n1=10^5, n2=10)
    myMatrix <- matrix(data = as.vector(myOutput), nrow = 3, ncol = 3)
    rownames(myMatrix) <- c(0.1,0.25,0.2)
    colnames(myMatrix) <- c(0.1,0.25,0.2)
    

    Now myMatrix has the output that you wanted. You can use it as input for other functions.