3

I have a following data frame

tdf <- structure(list(GO = c("Cytokine-cytokine receptor interaction", 
"Cytokine-cytokine receptor interaction|Endocytosis", "I-kappaB kinase/NF-kappaB signaling", 
"NF-kappa B signaling pathway", "NF-kappaB import into nucleus", 
"T cell chemotaxis"), PosCount = c(17, 18, 4, 5, 1, 2), shortgo = structure(c(7L, 
7L, 18L, 18L, 18L, 21L), .Label = c("TNF", "adaptive", "alpha", 
"apop", "beta", "chemokine", "cytokine", "death", "defense", 
"gamma", "immune response", "infla", "interleukin-1 ", "interleukin-10 ", 
"interleukin-12 ", "interleukin-18 ", "interleukin-6 ", "kappa", 
"migration", "stress", "taxis", "wound"), class = "factor")), .Names = c("GO", 
"PosCount", "shortgo"), class = "data.frame", row.names = c(NA, 
6L))

That looks like this:

> tdf
                                                  GO PosCount  shortgo
1             Cytokine-cytokine receptor interaction       17 cytokine
2 Cytokine-cytokine receptor interaction|Endocytosis       18 cytokine
3                I-kappaB kinase/NF-kappaB signaling        4    kappa
4                       NF-kappa B signaling pathway        5    kappa
5                      NF-kappaB import into nucleus        1    kappa
6                                  T cell chemotaxis        2    taxis

What I want to do is to split the data frame according to shortgo and then sort its GO member by PosCount, yielding this (handcrafted):

$cytokine
[1] Cytokine-cytokine receptor interaction|Endocytosis
[2] Cytokine-cytokine receptor interaction 

$kappa
[1] NF-kappa B signaling pathway
[2] I-kappaB kinase/NF-kappaB signaling 
[3] NF-kappaB import into nucleus

$taxis
[1] T cell chemotaxis

I'm stuck with this:

> split(tdf$GO,tdf$shortgo)
Error in split.default(tdf$GO, tdf$hsortgo) : 
  group length is 0 but data length > 0

How can I go about it?

Jaap
  • 71,900
  • 30
  • 164
  • 175
neversaint
  • 50,277
  • 118
  • 274
  • 437

2 Answers2

5

You can order your dataframe first before you split:

library(dplyr)
tdf <- tdf %>% group_by(shortgo) %>% arrange(desc(PosCount))

Then split:

ldf <- split(tdf$GO, tdf$shortgo, drop=TRUE)

Which gives the desired (ordered) output:

> ldf
$cytokine
[1] "Cytokine-cytokine receptor interaction|Endocytosis"
[2] "Cytokine-cytokine receptor interaction"            

$kappa
[1] "NF-kappa B signaling pathway"       
[2] "I-kappaB kinase/NF-kappaB signaling"
[3] "NF-kappaB import into nucleus"      

$taxis
[1] "T cell chemotaxis"

When you want to split your dataframe in a list of dataframes, you can use:

ldf <- split(tdf, tdf$shortgo, drop=TRUE)

A solution with base R (provided by @Henrik in the comments):

split(tdf$GO[order(tdf$shortgo, -tdf$PosCount)], tdf$shortgo, drop=TRUE)
Community
  • 1
  • 1
Jaap
  • 71,900
  • 30
  • 164
  • 175
  • 1
    Or if OP wish to stick with `base` functions: `split(tdf$GO[order(tdf$shortgo, -tdf$PosCount)], tdf$shortgo, drop = TRUE)` (you don't need `dplyr` to sort the data). – Henrik Apr 28 '15 at 08:35
  • @Henrik Thanx! I will include it in the answer. – Jaap Apr 28 '15 at 09:00
2

Using data.table, you can use setorder() to reorder your data.table by reference, and then group then as follows:

require(data.table)
ans = setorder(setDT(tdf), shortgo, -GO)[, .(GO_list = list(GO)), by=shortgo]

I'd recommend keeping it as such as it's easier to perform computations on it. But if you insist on your final structure, then you can do:

ans = setattr(ans$GO_list, 'names', as.character(ans$shortgo))

If you don't wish to reorder your original data by reference, you can do:

ans = setDT(tdf)[order(shortgo, -GO), .(GO_list = list(GO)), by=shortgo]
Community
  • 1
  • 1
Arun
  • 108,644
  • 21
  • 263
  • 366