I am attempting to run a collinearity test on detection data I have, through spearman rank and pearson tests. However, I have run into a problem I was hoping someone may be able to help me past (I am still fairly new to RStudio).
I successfully entered and read my detection data in R (all detection data have replicates of 5) and created a list of them using the list() function.
However, when I try to turn it into a matrix using the following code:
obs.det.n<-as.matrix(sapply(obs.det, as.numeric,2),nrow=50,ncol=5,byrow=TRUE)
I get the error "Error in lapply(X = X, FUN = FUN, ...) :
'list' object cannot be coerced to type 'double'".
Does anyone have any idea of what is going wrong and how to circumvent the issue?
Here is the data.frame(obs.det) for partial data in case that helps.
You should be able to just turn a data.frame into a matrix directly (it will have the effect of coercing all of the columns to the highest storage mode present according to the mode hierarchy, but these all appear to be numeric as they are, so,
obs.det.n <- as.matrix(obs.det)
EDIT:If the output shown is from the command,
data.frame(obs.det)
Then you would do,
as.matrix(data.frame(obs.det))
END EDIT
should be sufficient to do what you want, unless I am missing something in your data.
With regard to the actual error though, in the event anyone else lands here by searching for the same error.
as.matrix() does not accept and will ignore nrow, ncol, and bycol arguments, so you can simplify your code there.
sapply()generally only takes two arguments itself (simplify and USE.NAMES are the ones you can send to sapply(), anything else will be passed on to the function you intend to apply to the data), so the argument 2 is being passed to as.numeric() which isn't going to do anything with it.
sapply(), by default will simplify using the function simplify2array(), so as long as the result of each evaluation of the applied function returns a vector of the same type and length, the output will be an array (matrix) anyway, so as.matrix() is redundant.
Your data is already numeric, so I am not sure why you need the as.numeric() function you're using in the sapply() call.