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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:50:37+00:00 2026-06-17T21:50:37+00:00

I have a matrix that I would like to subset and eventually use to

  • 0

I have a matrix that I would like to subset and eventually use to make a plot. The data is a list of counts for specific blood markers for each patient in a population. It looks like this:

    df <- data.frame(MarkerID=c("Class","A123","A124"),
             MarkerName=c("","X","Y"),
             Patient.1=c(0,1,5),
             Patent.2=c(1,2,6),
             Patent.3=c(0,3,7),
             Patient.4=c(1,4,8))

I would like to make a data frame of all of the patients (columns 3-6) that have a class value of zero (1st row) and a second data frame of all of the patients with a class value of 1.

In the past I have used the subset function to select rows based on the values in a column, is it possible to select a subset of columns based on the values in a row?

I’ve tried this:

x <- subset(data, data[1,] == 0)

however, when I do dim(x) the number of columns is the same as dim(data) but the number of rows is different. Any ideas on how I can make this return just those columns whose value in row 1 is 0?

Roland,
Yes. You’re example df is what the data frame looks like. There are ~30,000 markers and >400 patients in the data frame so I didn’t post the dput(head(data)). Thanks for the reshaping tip, I’ll give that a try.

Your example code did work to subset the columns based on the rows

data[,c(TRUE,TRUE,data[1,-(1:2)]==1)]

on the data I was then able to get a data frame with all of the rows and only the columns with the indicated class.

  • 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-17T21:50:38+00:00Added an answer on June 17, 2026 at 9:50 pm

    Your data is nor arranged in a good way. It would be better to reshape it.

    In absence of input data this is just a guess:

    df <- data.frame(MarkerID=c("Class","A123","A124"),
                     MarkerName=c("","X","Y"),
                     Patient.1=c(0,1,5),
                     Patent.2=c(1,2,6),
                     Patent.3=c(0,3,7),
                     Patient.4=c(1,4,8))
    
    #  MarkerID MarkerName Patient.1 Patent.2 Patent.3 Patient.4
    #1    Class                    0        1        0         1
    #2     A123          X         1        2        3         4
    #3     A124          Y         5        6        7         8
    
    df[,c(TRUE,TRUE,df[1,-(1:2)]==0)]
    
    #  MarkerID MarkerName Patient.1 Patent.3
    #1    Class                    0        0
    #2     A123          X         1        3
    #3     A124          Y         5        7
    

    Here c(TRUE,TRUE,df[1,-(1:2)]==0) creates a logical vector, which is TRUE for the first two columns and for those columns, which have a 0 in the first row. Then I subset the columns based on this vector.

    df[,c(TRUE,TRUE,df[1,-(1:2)]==1)]
    
    #  MarkerID MarkerName Patent.2 Patient.4
    #1    Class                   1         1
    #2     A123          X        2         4
    #3     A124          Y        6         8
    

    This would reshape your data into a more common format (for statistical software):

    library(reshape2)  
    df2 <- merge(melt(df[1,],variable.name="Patient",value.name="class")[-(1:2)],
                 melt(df[-1,],variable.name="Patient"),all=TRUE)
    
    #    Patient class MarkerID MarkerName value
    #1  Patent.2     1     A123          X     2
    #2  Patent.2     1     A124          Y     6
    #3  Patent.3     0     A123          X     3
    #4  Patent.3     0     A124          Y     7
    #5 Patient.1     0     A123          X     1
    #6 Patient.1     0     A124          Y     5
    #7 Patient.4     1     A123          X     4
    #8 Patient.4     1     A124          Y     8
    

    You could then use subset:

    subset(df2,class==0)
    
    #    Patient class MarkerID MarkerName value
    #3  Patent.3     0     A123          X     3
    #4  Patent.3     0     A124          Y     7
    #5 Patient.1     0     A123          X     1
    #6 Patient.1     0     A124          Y     5
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a matrix in R that I would like to take a single
I have an NxM matrix in MATLAB that I would like to reorder in
I have a matrix for instance a=[12,2,4,67,8,9,23] and I would like a code that
I have a regular matrix (non-sparse) that I would like to convert to a
I have a large matrix that I would like to center: X <- matrix(sample(1:10,
I have a matrix that has 20 rows and 51 columns. I would like
I have a large matrix that I would like to convert to sparse CSR
I have a matrix of objects that contains data in this form: name A,2,name
If I have a matrix that is iterated horizontally and then vertically it would
I have a large matrix from which I would like to gather a collection

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.