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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:49:43+00:00 2026-06-01T03:49:43+00:00

I have a matrix in R that I would like to take a single

  • 0

I have a matrix in R that I would like to take a single random sample from each row. Some of my data is in NA, but when taking the random sample I do not want the NA to be an option for the sampling. How would I accomplish this?

For example,

a <- matrix (c(rep(5, 10), rep(10, 10), rep(NA, 5)), ncol=5, nrow=5)
a
     [,1] [,2] [,3] [,4] [,5]
[1,]    5    5   10   10   NA
[2,]    5    5   10   10   NA
[3,]    5    5   10   10   NA
[4,]    5    5   10   10   NA
[5,]    5    5   10   10   NA

When I apply the sample function to this matrix to output another matrix I get

b <- matrix(apply(a, 1, sample, size=1), ncol=1)
b

     [,1]
[1,]   NA
[2,]   NA
[3,]   10
[4,]   10
[5,]    5

Instead I do not want the NA to be capable of being the output and want the output to be something like:

b
     [,1]
[1,]   10
[2,]   10
[3,]   10
[4,]    5
[5,]   10
  • 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-01T03:49:44+00:00Added an answer on June 1, 2026 at 3:49 am

    There might be a better way but sample doesn’t appear to have any parameters related to NAs so instead I just wrote an anonymous function to deal with the NAs.

    apply(a, 1, function(x){sample(x[!is.na(x)], size = 1)})
    

    essentially does what you want. If you really want the matrix output you could do

    b <- matrix(apply(a, 1, function(x){sample(x[!is.na(x)], size = 1)}), ncol = 1)
    

    Edit: You didn’t ask for this but my proposed solution does fail in certain cases (mainly if a row contains ONLY NAs.

    a <- matrix (c(rep(5, 10), rep(10, 10), rep(NA, 5)), ncol=5, nrow=5)
    # My solution works fine with your example data
    apply(a, 1, function(x){sample(x[!is.na(x)], size = 1)})
    
    # What happens if a row contains only NAs
    a[1,] <- NA
    
    # Now it doesn't work
    apply(a, 1, function(x){sample(x[!is.na(x)], size = 1)})
    
    # We can rewrite the function to deal with that case
    mysample <- function(x, ...){
        if(all(is.na(x))){
            return(NA)
        }
        return(sample(x[!is.na(x)], ...))
    }
    
    # Using the new function things work.
    apply(a, 1, mysample, size = 1)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an NxM array in numpy that I would like to take the
I have a matrix of data (with row names and column names). I would
I have a matrix that has 20 rows and 51 columns. I would like
I have a huge matrix that I divided it into some sub matrices and
I have a .rdlc report and it has matrix. that matrix has some money
I have a scene that stores vertices in 3D space. What i would like
Say I have a matrix of ones and zeros, and I would like a
I have a matrix of one column and 6 rows. I would like to
We have an existing Traceability Matrix in Excel that has columns like: Project Business
I have a matrix A of size mXn and I would like to set

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.