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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:29:54+00:00 2026-05-26T16:29:54+00:00

Here is my small dataset set.seed(123) X1 <- rep(1:3, each = 4) X2 <-

  • 0

Here is my small dataset

set.seed(123)
X1 <- rep(1:3, each = 4)
X2 <- c(1:4, 1:4, 1:4)
Y <- rnorm (12, 4, 20)
myd <- data.frame(X1, X2, Y)

# "unmelten" form of the dataframe 
aggregate(Y ~ X1, myd, 'c')
  X1         Y.1         Y.2         Y.3         Y.4
1  1  -7.2095129  -0.6035498  35.1741663   5.4101678
2  2   6.5857547  38.3012997  13.2183241 -21.3012247
3  3  -9.7370570  -4.9132394  28.4816359  11.1962765

The actual data matrix is bigger in dimension. I want to write a function that use the melten form the data to a math like this:

 Example for X1, Y1.1 cell 

  (X1,Y.1) - sum(X1 row) - sum(Y.1 column) + grand total (= sum(Y))
    -7.2095129 - ( -7.2095129 - 0.6035498 + 35.1741663  + 5.4101678) - ( -7.2095129 + 6.5857547 -9.7370570) + sum ( all values in the able)

Other two example to make my point clear.

For X2, Y1.1 cell
     (X2,Y.1) - sum(X2 row) - sum(Y.1 column) + grand total (= sum(Y))

For X3, Y.3 cell  
     (X3,Y.3) - sum(X3 row) - sum(Y.3 column) + grand total (= sum(Y))

Similarly this will output a matrix of 3 x 4 size for each cell in the matrix.

I was trying to write a function to do this, but could get any idea. help please

  • 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-05-26T16:29:54+00:00Added an answer on May 26, 2026 at 4:29 pm

    Just do this

    z1 <- aggregate(Y ~ X1, myd, 'c')[,-1]
    ans <- z1 - rowSums(z1) - colSums(z1) + sum(z1)
    
             [,1]      [,2]      [,3]      [,4]
    [1,]  64.98307  65.92300  20.13181  34.45743
    [2,]  31.60013 106.46100  75.71199 -40.37646
    [3,] -17.03576  31.87768 108.41788  85.46648
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is a small example to illustrate my data: > df <- data.frame(subgroup=rep(paste(s,1:3, sep=),
I know my problem is simple but not for me. Here is small dataset.
I want make changes in big data.frame column names (names(mp)) with the help of
Here is small code to illustrate what I am seeing float floater = 59.999f;
I have a small situaton here. I'm building a custom CMS for one of
Ease of installation/use is the most important factor here - not performance. Small is
Java newbie here...caution! I've set this practice app up using this example as a
Every time I get a new data set the first thing I do is
I have a large data set dim(dt) [1] 422096 162 where dt is a
Scenario I am attempting to implement supervised learning over a data set within a

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.