Seqanswers Leaderboard Ad

Collapse

Announcement

Collapse
No announcement yet.
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • filtering - find-if commands

    Hi, Could someone please tell me how to do this simple search. I don't know much programming and this is beyond me. But I think it should be a simple find-if loop?
    Thanks so much. File below has my query


    # need to perform
    # if for each value j (X, Y, Z) in column2, value i in column1 = A and B and C and D, then output value j
    # so in this example, I should only get 'X' as the output
    # as only X has rows with all four value i (A and B and C and D)

    A X
    A X
    B X
    C X
    D X
    D X
    D X
    A Y
    A Y
    B Y
    A Z
    B Z
    C Z
    C Z
    C Z
    C Z
    C Z
    C Z
    C Z

  • #2
    Saving that file as "foo.txt", one could just use R:

    Code:
    #Read in the file
    d <- read.table("foo.txt", sep=" ")
    #Split it according to the second column
    dl <- split(d, d$V2)
    #Create a function to return TRUE/FALSE
    foo <- function(x) {
         if(length(unique(x$V1)) != 4) {
             FALSE
         } else if(!all(c("A","B","C","D") %in% unique(x$V1))) {
             FALSE
         } else {
             TRUE
        }
    }
    #Run things
    sapply(dl,foo)
    The output is:
    Code:
        X     Y     Z 
     TRUE FALSE FALSE
    Last edited by dpryan; 01-23-2014, 03:14 PM. Reason: Swap order

    Comment


    • #3
      Hi dpryan,

      This was really useful, thanks! I tried it on my actual data which is a bit more complex than the example above. I had to switch the %in% operator to come after the ("A","B',"C","D") instead of before and then it ran fine

      Thanks again!!

      Comment


      • #4
        True, I missed that. I updated my reply to incorporate that fix, should someone need it later.

        Comment

        Latest Articles

        Collapse

        • seqadmin
          Best Practices for Single-Cell Sequencing Analysis
          by seqadmin



          While isolating and preparing single cells for sequencing was historically the bottleneck, recent technological advancements have shifted the challenge to data analysis. This highlights the rapidly evolving nature of single-cell sequencing. The inherent complexity of single-cell analysis has intensified with the surge in data volume and the incorporation of diverse and more complex datasets. This article explores the challenges in analysis, examines common pitfalls, offers...
          06-06-2024, 07:15 AM
        • seqadmin
          Latest Developments in Precision Medicine
          by seqadmin



          Technological advances have led to drastic improvements in the field of precision medicine, enabling more personalized approaches to treatment. This article explores four leading groups that are overcoming many of the challenges of genomic profiling and precision medicine through their innovative platforms and technologies.

          Somatic Genomics
          “We have such a tremendous amount of genetic diversity that exists within each of us, and not just between us as individuals,”...
          05-24-2024, 01:16 PM

        ad_right_rmr

        Collapse

        News

        Collapse

        Topics Statistics Last Post
        Started by seqadmin, Yesterday, 06:54 AM
        0 responses
        10 views
        0 likes
        Last Post seqadmin  
        Started by seqadmin, 06-14-2024, 07:24 AM
        0 responses
        18 views
        0 likes
        Last Post seqadmin  
        Started by seqadmin, 06-13-2024, 08:58 AM
        0 responses
        16 views
        0 likes
        Last Post seqadmin  
        Started by seqadmin, 06-12-2024, 02:20 PM
        0 responses
        17 views
        0 likes
        Last Post seqadmin  
        Working...
        X