I'm trying to use the apply function to calculate sd across 30 columns in matrix, like this:
apply(M[,2:31], 1, sd)
The matrix dim 5287 x 31, where the frist column in the id. I get the error message:
Error in apply(M[, 2:31], 1, sd) : object 'M' not found
I don't understad why the matrix is not found? Could the function handle is if some oberservations is missing in some columns? I don't want the sd if observations is missing in some of my 30 columns.
If your matrix really exists this will work, though you'd be computing the standard deviations of the rows, columns would be MARGIN = 2.
Does this command return TRUE?
exists("M")
When you get the error are you running this code in an interactive environment (e.g. an R console or from a source editor) or are you trying to build a document (knitting to PDF or html)?
If M doesn't exist, that's your problem, you need to get M.
If M does exist and you are running the code interactively, then I am at a loss as to why you are getting that error.
If M does exist and you are getting this error while trying to build a document, this is actually very common. It means you ran some code interactively to create the M object in your environment, but there is no code in your R Markdown document which creates an M object (or it is located somewhere further down the document after you are trying to use it).
To answer the other part of your question, R won't care if data is missing, sd() will return NA for any column with missing data. If you wanted to ignore missingness and compute the standard deviations anyway, you would update the command to be,