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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:13:49+00:00 2026-06-18T05:13:49+00:00

i have some experience with R, but always struggle to write new code. i’ve

  • 0

i have some experience with R, but always struggle to write new code. i’ve found several very helpful posts here while working on my current project, but can’t seem to find the next step. here’s what i’ve done so far:

  • imported a 20×20 .csv of rankings; each column contains one instance of each integer from 1 to 20, so all colSums are 210. rowSums vary.

  • used a post on here to randomly sample 4 rows from the original matrix and put them into a new 4×20 matrix.

now, i need to sample 5 columns from each row, without replacement of columns. that is, i need to use each column only once and have five values in each row. (i don’t have a preference on whether this gives me a matrix with 20 values in the right places and 60 zeros, or if i get 4 vectors of 5 values. i guess i sort of want the matrix?)

if context helps, i’m trying to create groups based on topic rankings in a classroom. rows are topics and columns are voters (students). ultimately i want to create these random assignments in a for loop, and run the program many times to hopefully optimize the choices (by some measurement; obviously there are different ways to optimize) automatically rather than by staring at the original matrix, which is what i’ve done in the past.

this is my 4×20 matrix:

    J  E  I  S  A  N  H  T  M  B  D  K  O  G  P  L  Q  R  F  C
2   5  4  1  1  5 13  3  4 13 11 14 14 20  9 15  9 11 17  9 15
13 20 19 17 19 19  7  4 19  7  1  5  1 17 15 10  6  7 14  6  3
14 18  2 12 14 11 19 18 15 19  4  8 19  2  2 13  7  9  1 12 10
18  4  7 18  5 12 18  2 20  6  7 16 15  5 18  1 13  2 18 14 16

this is (one version of) what i want:

    J  E  I  S  A  N  H  T  M  B  D  K  O  G  P  L  Q  R  F  C
2   0  4  1  1  0  0  3  4  0  0  0  0  0  0  0  0  0  0  0  0
13  0  0  0  0  0  7  0  0  0  1  5  1  0  0  0  0  0  0  0  3
14  0  0  0  0 11  0  0  0  0  0  0  0  0  2  0  7  0  1 12  0
18  4  0  0  0  0  0  0  0  6  0  0  0  5  0  1  0  2  0  0  0
  • 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-18T05:13:50+00:00Added an answer on June 18, 2026 at 5:13 am

    You can use apply. The following command will randomly sample five values from each row and return a matrix of the results:

    apply(mat, 1, sample, 5)
    

    You might wish to transpose the returned matrix with t to match the original matrix.


    If you want to use every column only once, you can use the following command:

    mat[cbind(seq(nrow(mat)), sample(ncol(mat), 5 * nrow(mat)))]
    

    It will return a vector including the values.

    To match the desired output format (matrix including zeros and randomly chosen values), you can use the following strategy:

    # create an index of the values to be kept
    idx <- cbind(seq(nrow(mat)), sample(ncol(mat), 5 * nrow(mat)))
    
    # create a new matrix of zeroes
    mat2 <- matrix(0, ncol = ncol(mat), nrow = nrow(mat))
    
    # copy the values from the original matrix to the new one
    mat2[idx] <- mat[idx]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I have some experience with programming but I'm not very good with pointers.
Hey, i have some experience with MVC. but I'm new to rails. I'm using
I am a bit new with android but I have some java experience. So
I'm new to Java, but have some OOP experience with ActionScript 3, so I'm
I want to write some games, but I don't have any game development experience.
I'm new to Android and Java but do have some experience in Objective C
I have some experience using parallel extensions in .Net development, but I was looking
I have some experience with Java , C , databases , networking etc..But anything
I've got zero experience with Python. I have looked around some tutorial materials, but
Firstly, Im new to android but have years of various other programming experience on

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.