Hi List

I have a dataframe (~1,200,000 rows deep) and I'd like to conditionally reorder groups of rows in this dataframe. 

I would like to reorder any rows where the Chr.Strand column contains a '-' but reorder within subsets delineated by the Probe.Set.Name column.

# toy example ####

library(plyr)

negStrandGene <- data.frame(Probe.Set.Name = rep('ENSMUSG00000022174_at', 6), Chr = rep(14,6), Chr.Strand = rep('-', 6), Chr.From = c(54873546, 54873539, 54873533, 54873529, 54873527, 54873416), Probe.X = c(388,1634,2141,2305,882,960), Probe.Y = c(2112, 1773, 1045, 862, 971, 2160))

posStrandGene <- data.frame(Probe.Set.Name = rep('ENSMUSG00000047459_at', 6), Chr = rep(2, 6), Chr.Strand = rep('+', 6), Chr.From = c(155062277, 155062304, 155062305, 155062309, 155062326, 155062531), Probe.X = c(428, 1681, 2058, 1570, 1293, 2125), Probe.Y = c(1484, 2090, 893, 1082, 1435, 1008))

mapping <- rbind (negStrandGene, posStrandGene)

# define a function to do what we want
revSort <- function(df){

  if (unique(df$Chr.Strand == '-')) 
  return (df[order(df$Chr.From), ])
  else return (df)

}


# split the data with plyr, apply the function and recombine
test2 <- ddply(mapping, .(Probe.Set.Name), function(df) revSort(df)) # ok, cool works

So here the rows with the '-' if Chr.Strand are reordered whilst those with '+' are not.

My initial attempt using plyr is very inefficient and I wondered if someone could suggest something better.

Best

Iain

	[[alternative HTML version deleted]]

