tags:

views:

107

answers:

1

Hello,

I have asked this question before and received a solution, but my problem is somewhat varied from the original explanation

I have a data frame, like this:

nums<-c(5,7,8,9,10,3,2,1)
text<-c("Company a","Company b","Company c","Company d","Company a 09","Company b 09","Company c 09","Company d 09")
this <- data.frame()
this <- cbind(text,nums)

"Company a:d" = data from 2010, "Company a 09:d:09" = data from 2009. I'd like it to be sorted first by the numeric column from greatest to least and then by the string column. The only catch is that the string column has to show the 09' data underneath the 2010 data, like this:

"Company d"   9
"Company d 09" 1
"Company c"   8
"Company c 09" 2
"Company b"   7
"Company b 09" 3
"Company a"   5 
"Company a 09" 10

There's been a few suggestions from this question, but I can't replicate it for this, somewhat more complicated example.

I've uploaded some test data.

+2  A: 

How about this?

## Read in the data
foo <- read.csv("testdata.csv")
## Normalize names so that things work later.
foo$Company <- toupper(gsub(" *$", "", foo$Company))

## Split the data into 09 and not 09.
not.09 <- subset(foo, !grepl("09$", Company))
is.09 <- subset(foo, grepl("09$", Company))

## Figure out where the not09 should go (even indices)
not.09$Index <- 2 * order(not.09$Score, decreasing=TRUE)

## Find out where the 09s should go (odd indices)
is.09$Index <- not.09[match(is.09$Company, paste(not.09$Company, "09")),]$Index + 1

## Combine and sort
combined <- rbind(is.09, not.09)
combined <- combined[order(combined$Index),-4]
Jonathan Chang
This one works! thanks for the detailed comments too!
Brandon Bertelsen