1

I am wondering how to tidy the following:

First I gather a selection of columns into a tibble with three columns: strain (=grouping factor), params (names of parameters) and values (the actual values)

sel <- t_tcellact %>% select(strain, contains("nbr_")) %>% gather(params, values, nbr_DP:nbr_CD3p)

Then I perform multiple pairwise.t.test():

 test2 <- sel %>% bind_rows(sel) %>% split(.$params) %>% map(~ pairwise.t.test(x=.$values, g=.$strain, p.adj = "none"))

And the result is a list of the results from the pairwise.t.tests which I can start cleaning with:

test3 <- lapply(test2, tidy)

The list looks now like that:

$nbr_CD3p   

group1 group2        p.value

1    SKG Balb/c 0.000001849548

$nbr_DN_CD69nCD25n  

group1 group2   p.value 

1    SKG Balb/c 0.6295371

and so on....

From this I need a tibble with following columns: parameter (e.g. nbr_CD3p), group1, group2, p.value.

In this example I had only two groups, but I want to do it in a generic way also applicable when I have multiple groups.

Does anybody have an idea how to get to this point in an elegant way (without a loop)?

aosmith
  • 34,856
  • 9
  • 84
  • 118
robsn81
  • 11
  • 1

2 Answers2

1

You should be able to use bind_rows(), taking advantage of the .id argument:

test3 <- lapply(test2, tidy) %>%
     bind_rows(.id = 'parameter')

That will use the names of test2 as a new column named parameter in the data frame. All that said, replacing lapply with map_df() as aosmith suggested in a comment above should also work.

crazybilly
  • 2,992
  • 1
  • 16
  • 42
0

I found a way to do that:

test2 <- sel %>% bind_rows(sel) %>% split(.$params) %>% map(~ pairwise.t.test(x=.$values, g=.$strain, p.adj = "none")) %>% lapply(tidy) %>% do.call("rbind", .) %>% mutate(params = rownames(.)) %>% as_tibble()
robsn81
  • 11
  • 1
  • Have you tried `map_df` with `tidy` instead of `lapply`? That might get you there in one step if you use the `.id` argument. – aosmith Mar 24 '17 at 15:59