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
          Recent Advances in Sequencing Analysis Tools
          by seqadmin


          The sequencing world is rapidly changing due to declining costs, enhanced accuracies, and the advent of newer, cutting-edge instruments. Equally important to these developments are improvements in sequencing analysis, a process that converts vast amounts of raw data into a comprehensible and meaningful form. This complex task requires expertise and the right analysis tools. In this article, we highlight the progress and innovation in sequencing analysis by reviewing several of the...
          05-06-2024, 07:48 AM
        • seqadmin
          Essential Discoveries and Tools in Epitranscriptomics
          by seqadmin




          The field of epigenetics has traditionally concentrated more on DNA and how changes like methylation and phosphorylation of histones impact gene expression and regulation. However, our increased understanding of RNA modifications and their importance in cellular processes has led to a rise in epitranscriptomics research. “Epitranscriptomics brings together the concepts of epigenetics and gene expression,” explained Adrien Leger, PhD, Principal Research Scientist...
          04-22-2024, 07:01 AM

        ad_right_rmr

        Collapse

        News

        Collapse

        Topics Statistics Last Post
        Started by seqadmin, 05-07-2024, 06:57 AM
        0 responses
        12 views
        0 likes
        Last Post seqadmin  
        Started by seqadmin, 05-06-2024, 07:17 AM
        0 responses
        16 views
        0 likes
        Last Post seqadmin  
        Started by seqadmin, 05-02-2024, 08:06 AM
        0 responses
        22 views
        0 likes
        Last Post seqadmin  
        Started by seqadmin, 04-30-2024, 12:17 PM
        0 responses
        24 views
        0 likes
        Last Post seqadmin  
        Working...
        X