0

次のデータを検討してください

set.seed(123)

example.df <- data.frame( 
gene = sample(c("A", "B", "C", "D"), 100, replace = TRUE),
treated = sample(c("Yes", "No"), 100, replace = TRUE), 
resp=rnorm(100, 10,5), effect = rnorm (100, 25, 5))

遺伝子のレベルで比較され、治療によってグループ化されたときに、すべての変数の最大値を取得しようとしています。このように遺伝子の組み合わせを作ることができます。

combn(sort(unique(example.df$gene)), 2, simplify = T)

#     [,1] [,2] [,3] [,4] [,5] [,6]
#[1,] A    A    A    B    B    c   
#[2,] B    c    D    c    D    D   
#Levels: A B c D

編集:私が探している出力は、このようなデータフレームです

comparison   group    max.resp    max.effect
A-B          no       value1      value2
....
C-D          no       valueX      valueY
A-B          yes      value3      value4 
.... 
C-D          yes      valueXX     valueYY

治療によってグループ化された個々の遺伝子レベルごとに最大値を取得できますが...

max.df <- example.df %>% 
           group_by(treated, gene) %>% 
           nest() %>% 
           mutate(mod = map(data, ~summarise_if(.x, is.numeric, max, na.rm = TRUE))) %>% 
           select(treated, gene, mod) %>% 
           unnest(mod) %>% 
           arrange(treated, gene)

1 日以上この問題に取り組もうとしても、2 レベルの遺伝子 比較 ( A 対 B、A 対 C、A 対 D、B 対 C、B 対D、および C vs D ) によってグループ化され、扱います。

どんな助けでも大歓迎です。ありがとう。

4

1 に答える 1

1

解決策を見つけました。少し面倒かもしれませんが、より良い方法で更新します。時間はかかりません。

library(tidyverse)

最初に、可能な比較のために Gen1 と Gen2 の 2 つの列を持つデータフレームを生成しますcombn

GeneComp <- expand.grid(Gen1 = unique(example.df$gene), Gen2 = unique(example.df$gene)) %>% filter(Gen1 != Gen2) %>% arrange(Gen1)

次に、グループ化してループします

Comps <- list()
for(i in 1:nrow(GeneComp)){
  Comps[[i]] <- example.df %>% filter(gene == GeneComp[i,]$Gen1 | gene == GeneComp[i,]$Gen2) %>% # This line filters only the data with genes in the ith row
  group_by(treated) %>% # Then gorup by treated
  summarise_if(is.numeric, max) %>% # then summarise max if numeric
  mutate(Comparison = paste(GeneComp[i,]$Gen1, GeneComp[i,]$Gen2, sep = "-")) # and generate the comparisson variable
}

Comps <- bind_rows(Comps) # and finally join in a data frame

それがあなたが望むすべてをするかどうか私に知らせてください

一度だけデータを取得するために追加する

ここでは、遺伝子は文字列であり因子ではないことが重要なので、これを行う必要があるかもしれません

options(stringsAsFactors = FALSE)

example.df <- data.frame( 
  gene = c(sample(c("A", "B", "C", "D"), 100, replace = TRUE)),
  treated = sample(c("Yes", "No"), 100, replace = TRUE), 
  resp=rnorm(100, 10,5), effect = rnorm (100, 25, 5))

次に、再び引数をexpand.grid追加しますstringsAsFactors = F

GeneComp <- expand.grid(Gen1 = unique(example.df$gene), Gen2 = unique(example.df$gene), stringsAsFactors = F) %>% filter(Gen1 != Gen2) %>% arrange(Gen1)

Comparisson変数を貼り付けて両方の入力をソートするときにループ内で使用できるようになりました。これにより、行が複製されますがdistinct、最後に関数を使用すると、データが希望どおりになります。

Comps <- list()
for(i in 1:nrow(GeneComp)){
    Comps[[i]] <- example.df %>% filter(gene == GeneComp[i,]$Gen1 | gene == GeneComp[i,]$Gen2) %>% # This line filters only the data with genes in the ith row
    group_by(treated) %>% # Then gorup by treated
    summarise_if(is.numeric, max) %>% # then summarise max if numeric
    mutate(Comparison = paste(sort(c(GeneComp[i,]$Gen1, GeneComp[i,]$Gen2))[1], sort(c(GeneComp[i,]$Gen1, GeneComp[i,]$Gen2))[2], sep = "-")) # and generate the comparisson variable
}

Comps <- bind_rows(Comps) %>% distinct() # and finally join in a data frame
于 2019-03-06T17:13:54.673 に答える