Sd across columns


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.

Could anybody help me out with this?

Thank you a lot!

Regards Marit

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?


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,

apply(M[,2:31], 2, sd, na.rm = TRUE)

The problem reduces to

#> Error in eval(expr, envir, enclos): object 'M' not found

Created on 2020-08-31 by the reprex package (v0.3.0)

for the case in which M is not in the namespace. For M that is in namespace,

M <- array(1:3, c(2,4))
apply(M[, 2:4], 1, sd)
#> [1] 1 1

Created on 2020-08-31 by the reprex package (v0.3.0)

Thank you for help!

My M does exist, and I am running the code interactively. So, I'm stil stuck.

Does M exist in Global.env? In a fresh session?

I solved it by converting to dataframe and using transform function.

Thanks for your comments!

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.