I am a cardiologist and love coding in R - i am having a real issue with sorting a data frame and i suspect the solution is really easy!
I have a data frame with summary values from multiple studies df$study. Most studies have only one summary value (df$summary). However as you can see Study A has three summary values (df$no.of.estimate). See below
study <- c("E", "A", "F", "A", "B", "A", "C", "D")
no.of.estimate <- c(1, 2, 1, 3, 1, 1, 1, 1)
summary <- c(1, 2, 3, 5, 6 ,7 ,8 ,9)
df <- data.frame(study, no.of.estimate, summary)
So i want to sort the dataframe by df$summary - which is easy. However, if each study has more than one estimate then i want to group these studies together and appear in order using the "no.of.estimates" column.
So essentially the desired output is
study <- c("E", "A", "A", "A", "F", "B", "C", "D")
no.of.estimate <- c(1, 1, 2, 3, 1, 1, 1, 1)
summary <- c(1, 7, 2, 5, 3 ,6 ,8 ,9)
df <- data.frame(study, no.of.estimate, summary)
cbind, have created a matrix with columns as character class. Usedata.frame(study, no.of.estimate...)sudyandno.of.estimaterather only in case theno.of.estimatehas more than one value? It seems like you overcomplicating this a bit. It seems like you could just dodf[with(df, order(study, no.of.estimate)), ], though take a look on @akruns comment first.