Question

I'm relatively new to R and I'm trying to write a permutation to rotate my p-values in blocks of 10. Here is the head of my data.

> head(knockdown, n=10)
chrs position    pval gene_symbol  Site_Class
2L     4998 0.73842731     CG11023 UPSTREAM  
2L     4998 0.73842731      l(2)gl DOWNSTREAM
2L     5092 0.18879142     CG11023 UPSTREAM
2L     5092 0.18879142      l(2)gl DOWNSTREAM
2L     5095 0.15217914     CG11023 UPSTREAM
2L     5095 0.15217914      l(2)gl DOWNSTREAM
2L     5317 0.00000209     CG11023 UPSTREAM
2L     5317 0.05224209      l(2)gl DOWNSTREAM
2L     5372 0.64378453      l(2)gl DOWNSTREAM
2L     5372 0.64378453     CG11023 UPSTREAM

I would like to rotate pval in 10 row blocks 10 times, and for each rotation pull out information in gene_symboland Site_class relating to pval which are smaller than 1e-5. From this I would expect to see varying gene_symbol values coming through, however they will have kept their initial structure within the block, i.e if the significant p-value is located at row 7 in the first block, it will be located at row 17 in the first rotation and associated with a different gene_symbol. thanks for your help.

Was it helpful?

Solution

Get the index of the 110 p-values you're interested in

idx <- which(knockdown$pval < 1e-5)

A rotation is as follows (I think I've got those 1's correct -- R is 1-based); access the values at your new index, e.g.,

step <- 10000
for (i in seq_len(1000)) {
    idx <- ((idx + step - 1) %% nrow(knockdown)) + 1
    ## do something with these?
    knockdown$gene_symbol[idx]
}

OTHER TIPS

Using modulo division to split into groups of sequential 10-length blocks and then return those rows meeting the pval criterion:

knockdown <- rbind(knockdown, knockdown)
lapply( split(knockdown, 0:(nrow(knockdown)-1) %/% 10), 
       function(df) df[df$pval < 1e-5, ])
$`0`
  chrs position     pval gene_symbol Site_Class
7   2L     5317 2.09e-06     CG11023   UPSTREAM

$`1`
   chrs position     pval gene_symbol Site_Class
17   2L     5317 2.09e-06     CG11023   UPSTREAM
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top