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 9297101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T21:59:07+00:00 2026-06-18T21:59:07+00:00

this is a simple enough question that I’m suprised I can’t find any reference

  • 0

this is a simple enough question that I’m suprised I can’t find any reference to anyone having asked it before. It’s not the same as this, nor is it covered by this discussion.

I have a 4-d matrix (dimensions 16x10x15x39) with named dimnames (it’s what happens when you cast a dataframe from, e.g. a csv. You get to the names of the dimnames with names(dimnames(matrix)))

I then want to replace the columns (i.e. the first dimension) with fractions of the row total, so I do this:

matrix2 <- apply(matrix1, c(2,3,4), function(x){x/sum(x)})

But now names(dimnames(matrix2)) is blank for the first dimension. The other dimname names have been preserved.

So: how can I run apply over a matrix with named dimnames and keep the names of all the remaining dimensions?

A reproducable example

Here’s simple example of the problem. Just run the whole code and look at the last two lines.

x <- data.frame(
  name=c("bob","james","sarah","bob","james",
         "sarah","bob","james","sarah","bob",
         "james","sarah"),
  year=c("1995","1995","1995","1995","1995",
         "1995","2005","2005","2005","2005",
         "2005","2005"),
  sample_num=c("sample1","sample1","sample1",
               "sample2","sample2","sample2",
               "sample1","sample1","sample1",
               "sample2","sample2","sample2"),
  value=c(1,2,3,2,3,4,1,2,3,2,3,4)
  )
x <- cast(x, sample_num ~ name ~ year)
x_fractions <- apply(y,c(2,3),function(x){x / sum(x)})

names(dimnames(x))
names(dimnames(x_fractions))
  • 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-18T21:59:09+00:00Added an answer on June 18, 2026 at 9:59 pm

    I’m not quite sure what you’re looking for, but I think sweep function fits well for your goal. Try:

    result <- sweep(test, c(2,3,4), colSums(test), FUN='/')
    

    Where test is the array created by @user2068776. dimnames are preserved.

    dimnames(result)
    $a
    [1] "a1" "a2"
    
    $b
    [1] "b1" "b2"
    
    $c
    [1] "c1" "c2"
    
    $d
    [1] "d1" "d2"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this seems like a simple enough question but I can't seem to find a
This sounds like a simple enough question, but can't find the answer for the
I'm sure this question has a simple enough answer, but I can't seem to
I can't seam to find any simple enough tutorials or descriptions on clustering in
I hope this is a simple enough question for any SQL people out there...
This should seem simple enough, but can't figure it out. I was porting a
I'm hoping this is a simple enough question. I have FindBugs working in Eclipse.
I'm trying to simply prove here that this simple function isn't good enough to
I think there is a simple solution to this question, just not simple enough
This should be a simple answer. I'm just not knowledgeable enough in CSS to

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.