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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:50:28+00:00 2026-05-25T15:50:28+00:00

I have a matrix with 12 rows and 77 columns, but to simply lets

  • 0

I have a matrix with 12 rows and 77 columns, but to simply lets use:

p <- matrix(NA,5,7)  
p[1,2]<-0.3  
p[1,3]<-0.5  
p[2,4]<-0.9  
p[2,7]<-0.4  
p[4,5]<-0.6 

I want to know which columns are not “NA” per row, so what I would like to get would be something like:

[1] 2,3  
[2] 4  
[3] 0  
[4] 5  
[5] 0 

but if I do > which(p[]!="NA") I get [1] 6 11 17 24 32

I tried using a loop:

aux <- matrix(NA,5,7)  
for(i in 1:5) {  
    aux[i,]<-which(p[i,]!="NA")  
}

but I just get an error: number of items to replace is not a multiple of replacement length

Is there a way of doing this? Thanks in advance

  • 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-25T15:50:29+00:00Added an answer on May 25, 2026 at 3:50 pm

    Try:

    which( !is.na(p), arr.ind=TRUE)
    

    Which I think is just as informative and probably more useful than the output you specified, But if you really wanted the list version, then this could be used:

    > apply(p, 1, function(x) which(!is.na(x)) )
    [[1]]
    [1] 2 3
    
    [[2]]
    [1] 4 7
    
    [[3]]
    integer(0)
    
    [[4]]
    [1] 5
    
    [[5]]
    integer(0)
    

    Or even with smushing together with paste:

    lapply(apply(p, 1, function(x) which(!is.na(x)) ) , paste, collapse=", ")
    

    The output from which function the suggested method delivers the row and column of non-zero (TRUE) locations of logical tests:

    > which( !is.na(p), arr.ind=TRUE)
         row col
    [1,]   1   2
    [2,]   1   3
    [3,]   2   4
    [4,]   4   5
    [5,]   2   7
    

    Without the arr.ind parameter set to non-default TRUE, you only get the “vector location” determined using the column major ordering the R has as its convention. R-matrices are just “folded vectors”.

    > which( !is.na(p) )
    [1]  6 11 17 24 32
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a matrix that has 20 rows and 51 columns. I would like
so I want to write a matrix explorer which enables me to reorder rows
Given a matrix with 25 rows and 80 columns but with the attributes its
I have a matrix of one column and 6 rows. I would like to
I have a quadratic matrix(two-dimensional dynamic array of pointers) and need to change rows/columns
I have a large matrix from which I would like to randomly extract a
In Numpy, ix_() is used to grab rows and columns of a matrix, but
just a quick question, if I have a matrix has n rows and m
I have a matrix in SQL reporting and I would like it to print
I have a matrix in the type of a Numpy array. How would I

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.