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

  • Home
  • SEARCH
  • 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 7629339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:48:42+00:00 2026-05-31T05:48:42+00:00

Consider the following matrix, m <- matrix(letters[c(1,2,NA,3,NA,4,5,6,7,8)], 2, byrow=TRUE) ## [,1] [,2] [,3] [,4]

  • 0

Consider the following matrix,

m <- matrix(letters[c(1,2,NA,3,NA,4,5,6,7,8)], 2, byrow=TRUE)
##      [,1] [,2] [,3] [,4] [,5]
## [1,] "a"  "b"  NA   "c"  NA  
## [2,] "d"  "e"  "f"  "g"  "h" 

I wish to obtain the column indices corresponding to all non-NA elements, merged with the NA elements immediately following:

result <- c(list(1), list(2:3), list(4,5), 
                   list(1), list(2), list(3), list(4), list(5))

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-31T05:48:43+00:00Added an answer on May 31, 2026 at 5:48 am

    The column (and row) indicies of non-NA elements can be obtained with

    which(!is.na(m), TRUE)
    

    A full answer:

    Since you want to work row-wise, but R treats vector column-wise, it is easier to work on the transpose of m.

    t_m <- t(m)
    n_cols <- ncol(m)
    

    We get the array indicies as mentioned above, which gives the start point of each list.

    ind_non_na <- which(!is.na(t_m), TRUE)
    

    Since we are working on the transpose, we want the row indices, and we need to deal with each column separately.

    start_points <- split(ind_non_na[, 1], ind_non_na[, 2])
    

    The length of each list is given by the difference between starting points, or the difference between the last point and the end of the row (+1). Then we just call seq to get a sequence.

    unlist(
      lapply(
        start_points, 
        function(x)
        {
          len <- c(diff(x), n_cols - x[length(x)] + 1L)
          mapply(seq, x, length.out = len, SIMPLIFY = FALSE)
        }
      ), 
      recursive = FALSE
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider following make: all: a b a: echo a exit 1 b: echo b
Consider the following matrix, nc <- 5000 nr <- 1024 m <- matrix(rnorm(nc*nr), ncol=nc)
Consider the following matrix: sequence <- structure(list(C1 = c(2L, 9L, 3L, 9L, 1L, 8L,
Consider following code: My problem is: 1) I can't seem to cast the errors
Consider following string Some string with quotes and \pre-slashed\ quotes Using regex, I want
Consider following text: $content=<<<EOT { translatorID: f4a5876a-3e53-40e2-9032-d99a30d7a6fc, label: ACL, creator: Nathan Schneider, target: ^https?://(www[.])?aclweb\\.org/anthology-new/[^#]+,
Consider following 2 programs giving same error First calss: public class Testing { Testing
consider following: 1st APPROACH: public void f3() { f2(); f1(); } and this ...
Consider following scenario: I have RESTful URL /articles that returns list of articles user
Consider following program: static void Main (string[] args) { int i; uint ui; 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.