Multiple failed attempts converting variable from double to numeric

Hi, I'm trying to calculate correlation coefficients and p values for a dataset using rcorr. I got an error which I think means it doesn't like that some of the variables are stored as double:

fisherDataCor2<-rcorr(as.matrix(fisherData, ))
head(fisherDataCor2)

Warning in storage.mode(x) <- "double" : NAs introduced by coercion
Error in rcorr(as.matrix(fisherData)) :
NA/NaN/Inf in foreign function call (arg 1)

I then went back to convert the relevant variables to numeric and repeatedly failed. I tried using =numeric and got the invalid length argument error:

fisherData<- fisherData %>% mutate(MSL_PC1_16=numeric(MSL_PC1_16))

Show in New Window
Error: Problem with mutate() column MSL_PC1_16.
:information_source: MSL_PC1_16 = numeric(MSL_PC1_16).
x invalid 'length' argument

I then tried using as.numeric, and the code ran fine, but when I checked the data type was still double. I then tried several variations of that as suggested by other posts here, all of which "ran" without actually changing the data type.

fisherData%>%mutate(MSL_RC1_16=as.numeric(MSL_RC1_16))

fisherData$MSL_PC1_16<-as.numeric(fisherData$MSL_PC1_16)

fisherData[,'MSL_PC1_16]<-as.numeric(fisherData[,'MSL_PC1_16'])

Any further suggestions for what I can try would be much appreciated!

There is not a separate data type named double in R. That is the same as numeric. I suggest you look at the result of as.matrix(fisherData). Is it a matrix of numbers or characters?

It's definitely a matrix, there's a mix of numbers and characters. The code was running fine before I added a few new variables.

If having the data type as double is fine, do you know what I would need to do to fix the error with the correlation matrix that sent me down this road in the first place? (See top of original post).

Without seeing the data, I can only guess at solutions. If your data are a mixture of numbers and characters, that explains the warning. For example,

as.numeric(c(1,2,"A",4))
[1]  1  2 NA  4
Warning message:
NAs introduced by coercion 

Can you post your data fisherData? Posting the output of dput(fisherData) would be useful, if it isn't too big.

Unfortunately the data isn't mine, its my boss's and she's generally very protective of data before its published so I probably shouldn't. For now I think I'll just exclude the variables that are causing issues and do something else with them, because its just preliminary diagnostics. Thanks so much for your help though! Really appreciate it :slight_smile:

If you have a mixture of "numbers and characters" in the matrix then all the elements in the matrix will be character (see example below) and this explains @ FJCC example. You cannot convert the alphanumerics to numerals and so R is coercing them to NA

xx <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2)
str(xx)

yy <-   matrix(c(1, 2, 3, 4, 5, "A"), nrow = 2)
str(yy)

as.numeric(yy)

Is there a specific reason that the data is being stored in matrices? Usually, assuming the numeric data and character data are in separate columns one would use data.frames to store the data.

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.