Occupancy code: error in lapply

Hello,

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.

  dh.R1.dh    dh.R2.dh      dh.R3.dh   dh.R4.dh   dh.R5.dh    

1 0.2041685 0.3336424 0.2950844 0.2527576 0.5325092
2 0.2041685 0.3336424 0.2950844 0.2527576 0.5325092
3 0.2041685 0.3336424 0.2950844 0.2527576 -1.8395773
4 0.2041685 0.3336424 0.2950844 0.2527576 0.5325092
5 -4.7979587 0.3336424 0.2950844 0.2527576 -1.8395773
6 0.2041685 0.3336424 0.2950844 0.2527576 0.5325092
7 0.2041685 0.3336424 0.2950844 0.2527576 0.5325092
9 0.2041685 0.3336424 0.2950844 0.2527576 -1.8395773
10 0.2041685 0.3336424 0.2950844 0.2527576 0.5325092
11 0.2041685 -2.9360532 0.2950844 0.2527576 0.5325092
12 0.2041685 0.3336424 0.2950844 0.2527576 0.5325092

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.

  1. as.matrix() does not accept and will ignore nrow, ncol, and bycol arguments, so you can simplify your code there.
  2. 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.
  3. 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.
  4. Your data is already numeric, so I am not sure why you need the as.numeric() function you're using in the sapply() call.
1 Like

Also on the exemplary data

as.numeric(as.matrix(object,nrow=11,ncol=5,byrow=TRUE),2) 

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.