Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8805639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:55:08+00:00 2026-06-14T01:55:08+00:00

I just wrote a very basic function for the z tranformation in a Matrix

  • 0

I just wrote a very basic function for the z tranformation in a Matrix over all columns. It looks like this:

sapply(MyObject, function(x){(x-mean(x))/sd(x)})

I randomly checked my function for some cells within the Matrix and it seems to work fine. Still I wanted to confirm the function is okay, because I’m very new to R and I could not find any good examples on the internet.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T01:55:10+00:00Added an answer on June 14, 2026 at 1:55 am

    You should look at scale() which does this for you.

    Your function is close to being correct; you should add na.rm = TRUE to both the sd() and mean() function calls.

    I would write (if not using scale()) the function using sweep() instead of the sapply(). E.g.

    ztran <- function(x, na.rm = TRUE) {
        mns <- colMeans(x, na.rm = na.rm)
        sds <- apply(x, 2, sd, na.rm = na.rm)
        x <- sweep(x, 2, mns, "-")
        x <- sweep(x, 2, sds, "/")
        x
    }
    

    In use we have

    > df <- data.frame(matrix(1:9, ncol = 3))
    > ztran(df)
      X1 X2 X3
    1 -1 -1 -1
    2  0  0  0
    3  1  1  1
    > scale(df)
         X1 X2 X3
    [1,] -1 -1 -1
    [2,]  0  0  0
    [3,]  1  1  1
    attr(,"scaled:center")
    X1 X2 X3 
     2  5  8 
    attr(,"scaled:scale")
    X1 X2 X3 
     1  1  1
    

    sweep is a very useful vectorised tool for this sort of operation. Notice also that sapply() simplifies to a matrix, which may not be what you wanted. sweep() doesn’t do this:

    > class(ztran(df))
    [1] "data.frame"
    > class(sapply(df, function(x){(x-mean(x))/sd(x)}))
    [1] "matrix"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a very basic question. I'm just on my mission to learn ASP.NET
I just wrote a very simple perl tk script to read a huge text
Just wrote a web service and try to return columns of a sharepoint list
I just wrote this into my WebForms .aspx: <span id=GenerateChartButton runat=server></span> I went to
I just wrote a stored function to calculate the working days between two dates.
I'm really confused, I just wrote this x = 0 y = 42 #Can
I am a systems programmer, so i just know some basic css/html. I like
This might be very basic question to some but I am struggling for quite
Okay, so I wrote this just under a decade ago: http://meyerweb.com/eric/tools/color-blend/ I'm pretty sure
Please keep in mind that I am VERY new to all of this javascript/node.js

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.