NAs introduced by coercion


I am trying to compute a partial correlation using the pcor function from the ggm package.

This is my R code:

I want the correlation between total.score and global.score controlling for age. GL is the name of the data.

I keep getting this message: "In var(GL) : NAs introduced by coercion" However, I don't have any NA's in my data.

What should I do? I am new to R and would appreciate your help.


From the ggm docs, it looks like the pcor() function, pcor(u, S) takes arguments:

  • u: a vector of integers of length > 1. The first two integers are the indices of variables the correlation of which must be computed. The rest of the vector is the conditioning set.
  • S: a symmetric positive definite matrix, a sample covariance matrix.

Not knowing what your data looks like, it's hard to say, but NAs by coersion occur when, for example, you try to convert a character to numeric.

#> Warning: NAs introduced by coercion
#> [1] NA

Given that the arguments for pcor() are numeric, you might be passing character strings where the function requires numeric.

It will help us help you if you can put your issue in the form of a self-contained reprex (short for reproducible example)


If you've never heard of a reprex before, you might want to start by reading the help page. The reprex dos and don'ts are also useful.

What to do if you run into clipboard problems

If you run into problems with access to your clipboard, you can specify an outfile for the reprex, and then copy and paste the contents into the forum.

reprex::reprex(input = "fruits_stringdist.R", outfile = "")

For pointers specific to the community site, check out the reprex FAQ, linked to below.


Thank you for the response. I've never heard of a reprex before, so it may take me a little time to figure out. I'll give it a try, though.


Even without the reprex, make sure that your input arguments are valid for the function that you're using. You can learn more about the argument formats in R: Integer Vectors here, and Covariance Matrices here. There's also a function in the matrixcalc package, is.positive.definite(), that you could use to test that your argument S is a positive definite matrix.