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

The Archive Base Latest Questions

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

When I run a simple for loop to compute X number of permutations of

  • 0

When I run a simple for loop to compute X number of permutations of a vector, the sample() function returns the same permutation for each iteration.

Below is my code:

options <- commandArgs(trailingOnly=T)
labels <- read.table(options[2], header=F)
holder <- c()

for (i in 1:options[1]){

    perm <- sample(labels[,2:ncol(labels)], replace=F)
    perm <- cbind(as.character(labels[1]), perm)
    holder <- rbind(holder, perm)

}

write.table(holder, file=options[3], row.names=F, col.names=F, quote=F, sep='\t')

Is there a reason why this is so? Is there another simple way to generate say 1000 permutations of a vector?

*Added after comment – a replicable example*

vec <- 1:10
holder <-c()
for (i in 1:5){
    perm <- sample(vec, replace=F)
    holder <- rbind(holder, perm)
}

> holder
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
perm    3    2    1   10    9    6    7    4    5     8
perm    5    8    2    3    4   10    9    1    6     7
perm   10    7    3    1    4    2    5    8    9     6
perm    9    5    2    8    3    1    6   10    7     4
perm    3    7    5    6    8    2    1    9   10     4

And this works fine! I guess I have a bug somewhere! My input is perhaps in a mess.

Thanks,
D.

Thanks,
D.

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

    For a reproducible example, just replace options[1] with a constant set and labels to a built-in or self-specified data frame. (By the way, neither are great variable names being base functions.) Just looking at the inner part of your for loop, you shuffle all but the first column of a data.frame. This works as you expect. Put print(names(perm)) in after finishing making perm and you will see. You then rbind this data frame to the previous results. rbind, recognizing it is working with data frames, helpfully reshuffles the column order of the different data frames so that the column names line up (which, generally, is what you would want it to do; the name of the column defines which one it is and you would want to extend each column appropriately.)

    The problem is that you are doing permutations on columns of a data frame, not “of a vector” as you seem to think.

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

Sidebar

Related Questions

I want to run a simple JavaScript function on a click without any redirection.
I am using the .Net XslCompiledTranform to run some simple XSLT (see below for
I need to setup NACHOS java version in Linux and run some simple sample
I'm trying to create a simple loop that adds a random number of stars,
When I run some simple while loop code in linux user space and kernel
I am trying to program a simple loop to run through the user selected
I am trying to run a simple shell command that runs and returns text
I am trying to run a simple multiple processes application in Python. The main
I am trying to run a simple SQLITE application on Windows Mobile developed with
I'm trying to run a simple query with $this->db in Kohana, but am running

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.