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

  • Home
  • SEARCH
  • 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 8160085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:08:03+00:00 2026-06-06T18:08:03+00:00

In R: I have a matrix with one column classified by 8 types: a,

  • 0

In R: I have a matrix with one column classified by 8 types: a, b, c, d, e, f, g, h. I need to do different computations for each type using the data in the other columns. I want to use a switch() function to automate the looping through each type and state the difference calculations for each type; HOWEVER, everything I am seeing online only shows examples of the syntax for one line switch computations for each switch.

Here is an example using the code provided in the switch() help. I know that mean() is a function, but let’s just say for this example, that it isn’t a function, because I just want to illustrate that I do not know the syntax (and it’s not clearly stated in my research online):

centre <- function(x, type) {
  switch(type,
     mean = {
           total.sum<-sum(type)
           mean = total.sum/length(type)
     },
     median = median(x),
     trimmed = mean(x, trim = .1))
}
  • 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-06T18:08:04+00:00Added an answer on June 6, 2026 at 6:08 pm

    I think the miscommunication is coming from a typo in your example:

     mean = {
           total.sum<-sum(type)
           mean = total.sum/length(type)
     },
    

    should be

     mean = {
           total.sum<-sum(x)
           mean = total.sum/length(x)
     },
    

    If you make this change, it behaves exactly how you would expect it to.

    ETA: I’m not sure what the issue is in your comment. Please try the following code:

    set.seed(1)
    
    centre <- function(x, type) {
      switch(type,
         mean = {
               total.sum<-sum(x)
               mean = total.sum/length(x)
         },
         median = median(x),
         trimmed = mean(x, trim = .1))
    }
    
    x <- rcauchy(10)
    print(centre(x, "mean"))
    print(centre(x, "median"))
    print(centre(x, "trimmed"))
    

    The output is:

    [1] -0.4844658
    [1] -0.236111
    [1] -0.3632328
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a matrix with each column represents a feature over time. I need
Assume I have one matrix with two columns. Every column has thousands of values.
I have a cell matrix with two columns (no headers). Column one contains ticker
I have one column matrix with n rows. I want difference between row 2
I have a 40x16 matrix or 8 5x16 one below the other i.e. aligned
I have a matrix of data (the columns represent time, and the rows spectrum
I have a matrix of one column and 6 rows. I would like to
I have a matrix of data (with row names and column names). I would
Suppose I have a two column matrix. How do I pack the columns into
I have two matrix extracted from different csv files, One is like > matrix1[1:6,]

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.