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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:59:17+00:00 2026-05-30T19:59:17+00:00

I would like to find unique combinations of missing observations in matrix rows by

  • 0

I would like to find unique combinations of missing observations in matrix rows by a group variable.

I can do so with the example data set by using the sequence of subset, cbind and rbind commands shown to generate the matrix u3.

However, I suspect there is a much better way that would not involve ‘manually’ subsetting the matrix for each level of the group variable. I have tried using the tapply command at the bottom, but cannot get it to work.

Thank you sincerely for any suggestions.

my.data  <- matrix(c( 

            1, 0, 1, 1, 1,
            NA, 1, 1, 0, 1,
            NA, 0, 0, 0, 1,
            NA, 1,NA, 1, 1,
            NA, 1, 1, 1, 1,
             0, 0, 1, 0, 1,
            NA, 0, 0, 0, 1,
             0,NA,NA,NA, 1,
             1,NA,NA,NA, 1,
             1, 1, 1, 1, 1,
            NA, 1, 1, 0, 1,

            1, 0, 1, 1, 2,
            1, 1, NA, 0, 2,
            NA, NA, NA, 0, 2,
            NA, NA,NA, 1, 2,
             1, 1, 1, NA, 2,
             0, 0, 1, 0, 2,
            NA, 0, 0, 0, 2,
             0,NA,NA,NA, 2,
             1,NA,NA,NA, 2,
             1, 1, 1, 1, 2,
             0, 1, 1, NA, 2

), 
nrow=22, byrow=T, 
dimnames = list(NULL, c("c1","c2","c3","c4","my.group")))

my.data <- as.data.frame(my.data)
my.data

g1 <- subset(my.data, my.data$my.group==1)
u1 <- unique( is.na(g1[1:4]) )
u1 <- cbind(1,u1)

g2 <- subset(my.data, my.data$my.group==2)
u2 <- unique( is.na(g2[1:4]) )
u2 <- cbind(2,u2)

u3 <- rbind(u1,u2)
u3


tapply(my.data[,1:4], my.data$my.group, function(x) {unique(is.na(x), 'rows') } )

Here is the matrix u3:

     c1 c2 c3 c4
1  1  0  0  0  0
2  1  1  0  0  0
4  1  1  0  1  0
8  1  0  1  1  1
12 2  0  0  0  0
13 2  0  0  1  0
14 2  1  1  1  0
16 2  0  0  0  1
18 2  1  0  0  0
19 2  0  1  1  1
  • 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-30T19:59:19+00:00Added an answer on May 30, 2026 at 7:59 pm

    You can use the plyr package for this, it’s fantastic for “apply this function to each group”-type applications. In particular, the function ddply:

    library(plyr)
    u3 <- ddply(my.data,.(my.group),
          function(df)
              data.frame(unique(is.na(df[1:4])))
          )
    

    Then u3 looks like this:

       my.group    c1    c2    c3    c4
    1         1 FALSE FALSE FALSE FALSE
    2         1  TRUE FALSE FALSE FALSE
    3         1  TRUE FALSE  TRUE FALSE
    4         1 FALSE  TRUE  TRUE  TRUE
    5         2 FALSE FALSE FALSE FALSE
    6         2 FALSE FALSE  TRUE FALSE
    7         2  TRUE  TRUE  TRUE FALSE
    8         2 FALSE FALSE FALSE  TRUE
    9         2  TRUE FALSE FALSE FALSE
    10        2 FALSE  TRUE  TRUE  TRUE
    

    You could do as.matrix(u3) to get the numerical matrix.

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

Sidebar

Related Questions

I would like to find all the rows in a table and match on
I have N Lists I'd like to find unique combinations of. I've written it
I would like to build (or find) a php reporting library that I can
So I have a boost::multi_index_container with multiple non-unique indexes. I would like to find
I would like to do something like: find . -iname *Advanced*Linux*Program* -exec kpdf {}
I would like to find out safe ways of implementing three dimensional arrays of
I would like to find out if a particular python datetime object is older
I would like to find out where I would be able to find the
I would like to find out what is the most significant differences of MS
I would like to find out, in JavaScript, which element currently has focus. I've

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.