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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:24:13+00:00 2026-05-24T10:24:13+00:00

I have another question for the brilliant minds out there (this site is so

  • 0

I have another question for the brilliant minds out there (this site is so addictive).

I am running some simulations on a matrix and I have nested for loops for this purpose. The first creates a vector that increases by one each time a loop cycles. The nested loop is running simulations by randomizing the vector, attaching it to the matrix, and calculating some simple properties on the new matrix. (For an example, I used properties that will not vary in the simulations, but in practice I require the simulations to get a good idea of the impact of the randomized vector.) The nested loop runs 100 simulations, and ultimately I want only the column means of those simulations.

Here’s some example code:

property<-function(mat){                       #where mat is a matrix
  a=sum(mat) 
  b=sum(colMeans(mat))
  c=mean(mat)
  d=sum(rowMeans(mat))
  e=nrow(mat)*ncol(mat)
  answer=list(a,b,c,d,e)
  return(answer)
  }

x=matrix(c(1,0,1,0, 0,1,1,0, 0,0,0,1, 1,0,0,0, 1,0,0,1), byrow=T, nrow=5, ncol=4)

obj=matrix(nrow=100,ncol=5,byrow=T)            #create an empty matrix to dump results into

for(i in 1:ncol(x)){                           #nested for loops
  a=rep(1,times=i)                             #repeat 1 for 1:# columns in x
  b=rep(0,times=(ncol(x)-length(a)))           #have the rest of the vector be 0
  I.vec=append(a,b)                            #append these two for the I vector
    for (j in 1:100){
      I.vec2=sample(I.vec,replace=FALSE)       #randomize I vector
      temp=rbind(x,I.vec2)
      prop<-property(temp)
      obj[[j]]<-prop
      }
  write.table(colMeans(obj), 'myfile.csv', quote = FALSE, sep = ',', row.names = FALSE)
  }

The problem I am encountering is how to fill in the empty object matrix with the results of the nested loop. obj ends up as a vector of mostly NAs, so it is clear that I am not assigning the results properly. I want each cycle to add a row of prop to obj, but if I try

obj[j,]<-prop

R tells me that there is an incorrect number of subscripts on the matrix.

Thank you so much for your help!

EDITS:
Okay, so here is the improved code re the answers below:

property<-function(mat){                       #where mat is a matrix
  a=sum(mat)
  b=sum(colMeans(mat))
  f=mean(mat)
  d=sum(rowMeans(mat))
  e=nrow(mat)*ncol(mat)
  answer=c(a,b,f,d,e)
  return(answer)
  }

x=matrix(c(1,0,1,0, 0,1,1,0, 0,0,0,1, 1,0,0,0, 1,0,0,1), byrow=T, nrow=5, ncol=4)

obj<-data.frame(a=0,b=0,f=0,d=0,e=0)            #create an empty dataframe to dump results into
obj2<-data.frame(a=0,b=0,f=0,d=0,e=0)

for(i in 1:ncol(x)){                           #nested for loops
  a=rep(1,times=i)                             #repeat 1 for 1:# columns in x
  b=rep(0,times=(ncol(x)-length(a)))           #have the rest of the vector be 0
  I.vec=append(a,b)                            #append these two for the I vector
    for (j in 1:100){
      I.vec2=sample(I.vec,replace=FALSE)       #randomize I vector
      temp=rbind(x,I.vec2)
      obj[j,]<-property(temp)
      }
  obj2[i,]<-colMeans(obj)
  write.table(obj2, 'myfile.csv', quote = FALSE,
  sep = ',', row.names = FALSE, col.names=F, append=T)
  }

However, this is still glitchy, as the myfile should only have four rows (one for each column of x), but actually has 10 rows, with some repeated. Any ideas?

  • 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-24T10:24:14+00:00Added an answer on May 24, 2026 at 10:24 am

    Your property function is returning a list. If you want to store the numbers in a matrix, you should have it return a numeric vector:

    property <- function(mat)
    {
      ....
      c(a, b, c, d, e)  # probably a good idea to rename your "c" variable
    }
    

    Alternatively, instead of defining obj to be a matrix, make it a data.frame (which conceptually makes more sense, as each column represents a different quantity).

    obj <- data.frame(a=0, b=0, c=0, ...)
    for(i in 1:ncol(x))
      ....
      obj[j, ] <- property(temp)
    

    Finally, note that your call to write.table will overwrite the contents of myfile.csv, so the only output it will contain is the result for the last iteration of i.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have another question for the masses out there regarding trying to input values
Another question related to this one . I have a List<SortableObjects> that is the
I have already posted another question about this, but no one seemed to know
Ok I have another question and I'm a beginner at this. I have this
I have another question, which i'll be asking shortly, but to ask this question
Hey guys i have another question about dojo. This time it has to do
After trying this solution and getting one step further i have another question regarding
I have another question, not sure if this would be accomplished via PHP or
I have another question kind of relative to this but I'd like to separate
i have another question on getting my XML serialization neat, which i can't seem

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.