Search code examples
rprintingrnotebook

How to print specific rows in an r notebook document


I have recently started using R notebook to annotate my script and share information with colleagues. I have a data frame similar to data created below:

t <- data.frame(test=c("FirstPt_vs_LastPt", "FirstPt_vs_First3", "FirstPt_vs_First5", 
       "FirstPt_vs_First7", "FirstPt_vs_First10"))
n <- 5
test <- do.call("rbind", replicate(n,t,simplify=F))
Element <- rep(c("Copper", "Lead", "Zinc", "Bor", "Mag"), each = 5)
Region <- rep("alpha", 25)
p.value <- data.frame(p.value=sample(1:100,50,replace = T))

data <- cbind(test, Element, Region, p.value)

those are all the observations for Region==Alpha. In the same data frame, I have the same list of observations for several other Regions. For example, we will say the other Regions include Beta, and Charlie. I have been using: subset(data, test == "FirstPt_vs_LastPt" & Region == "Alpha") to print the values of FirstPt_vs_LastPt in Region == Alpha. Then I have to make a new code chunk, type the same thing, and change Alpha to Beta to get the results for that region.

Is there a way to collect all the rows under test that are FirstPt_vs_LastPt and print the results for each unique Region like this?:

Alpha

test                  Element   p.value
FirstPt_vs_LastPt      Copper    … 
FirstPt_vs_LastPt      Lead      … 
FirstPt_vs_LastPt      Zinc      …
FirstPt_vs_LastPt      Bor       … 
FirstPt_vs_LastPt      Mag       … 

Beta

test                  Element   p.value
FirstPt_vs_LastPt      Copper    … 
FirstPt_vs_LastPt      Lead      … 
FirstPt_vs_LastPt      Zinc      …
FirstPt_vs_LastPt      Bor       … 
FirstPt_vs_LastPt      Mag       … 

Charlie

test                  Element   p.value
FirstPt_vs_LastPt      Copper    … 
FirstPt_vs_LastPt      Lead      … 
FirstPt_vs_LastPt      Zinc      …
FirstPt_vs_LastPt      Bor       … 
FirstPt_vs_LastPt      Mag       … 

And can the output be printed all on one page, meaning I don't the output to have multiple pages to scroll through, it would make more sense for my readers to see them all on one page.


Solution

  • You can use the split function split(data, Region) which will return something like this:

    $alpha
                     test Element Region p.value
    1   FirstPt_vs_LastPt  Copper  alpha      28
    2   FirstPt_vs_First3  Copper  alpha      88
    3   FirstPt_vs_First5  Copper  alpha      16
    4   FirstPt_vs_First7  Copper  alpha       8
    ...
    
    $beta
                     test Element Region p.value
    31  FirstPt_vs_LastPt  Copper   beta      44
    32  FirstPt_vs_First3  Copper   beta      20
    33  FirstPt_vs_First5  Copper   beta      89
    34  FirstPt_vs_First7  Copper   beta      87
    

    I modified your code to get the 'beta' into the dataframe. Also, try to avoid using function names like t for your variable names.