2

最小値と最大値を取得すると同時に、平均化、プーリング、および複雑なインデックス作成を試みています。開始するには、これは例data.frameです:

ID      ID2         probe       chrom   strand txStart  txEnd
Rest_3  uc001aah.4  8044649     chr1    0      14361    29370
Rest_4  uc001aah.4  7911309     chr1    0      14361    29370    
Rest_5  uc001aah.4  8171066     chr1    0      14361    29370           
Rest_6  uc001aah.4  8159790     chr1    0      14361    29370   

Rest_17 uc001abw.1  7896761     chr1    0      861120   879961
Rest_18 uc001abx.1  7896761     chr1    0      871151   879961

ID2列で重複を見つけていduplicatedました:uc001aah.4には4つの重複があります。しかし、私が必要としている方法と、方法がわからないのは、uc001aah.4 のエントリを 1 つだけ持ち、プローブ列 (+ その他のエントリ) を 1 つのセル (Excel に関して) にプールする8044649, 7911309, 8171066, 8159790ことです。最後は次のようになります。

ID                              ID2         probe                                   chrom   strand txStart  txEnd
Rest_3,Rest_4, Rest_5, Rest_6   uc001aah.4  8044649, 7911309, 8171066, 8159790      chr1    0      14361    29370

ただし、重複はプローブ列にも当てはまります。

ID      ID2         probe       chrom   strand txStart  txEnd
Rest_17 uc001abw.1  7896761     chr1    0      861120   879961
Rest_18 uc001abx.1  7896761     chr1    0      871151   879961

したがって、ここでは、列 txStart の最小値と列 txEnd の最大値を取得しながら、ID と ID2 をプールする必要があります。

ID                  ID2                     probe       chrom   strand txStart  txEnd
Rest_17, Rest_18    uc001abw.1, uc001abx.1  7896761     chr1    0      861120   879961

これが多くの質問をしていることはわかっていますが、最初の問題でこれを行う方法を教えていただければ、それを2番目の問題に適用する方法を理解できると確信しています.

4

2 に答える 2

2

を使用したソリューションdata.table:

require(data.table)
dt <- data.table(df)
> dt
#         ID        ID2   probe chrom strand txStart  txEnd
# 1:  Rest_3 uc001aah.4 8044649  chr1      0   14361  29370
# 2:  Rest_4 uc001aah.4 7911309  chr1      0   14361  29370
# 3:  Rest_5 uc001aah.4 8171066  chr1      0   14361  29370
# 4:  Rest_6 uc001aah.4 8159790  chr1      0   14361  29370
# 5: Rest_17 uc001abw.1 7896761  chr1      0  861120 879961
# 6: Rest_18 uc001abx.1 7896761  chr1      0  871151 879961

# step 1: remove duplicate ID2 and concatenate ID and probe.
# Note: here I assume that if ID2 is same, then so will be chrom, 
# strand, txStart and txEnd. If not, you can modify this similar 
# to what is in step 2.
dt.out <- dt[, lapply(.SD, function(x) paste(x, collapse=",")), 
          by=c("ID2", "chrom", "strand", "txStart", "txEnd")]

#           ID2 chrom strand txStart  txEnd                          ID                           probe
# 1: uc001aah.4  chr1      0   14361  29370 Rest_3,Rest_4,Rest_5,Rest_6 8044649,7911309,8171066,8159790
# 2: uc001abw.1  chr1      0  861120 879961                     Rest_17                         7896761
# 3: uc001abx.1  chr1      0  871151 879961                     Rest_18                         7896761

# step 2: remove duplicate probe and concatenate others, get min(txStart) and max(txEnd)
dt.out <- dt.out[ ,list(ID=paste(ID, collapse=","), ID2=paste(ID2, collapse=","), 
                       txStart=min(txStart), txEnd=max(txEnd)), 
                       by=c("probe", "chrom", "strand")]

#                              probe chrom strand                          ID                   ID2 txStart  txEnd
# 1: 8044649,7911309,8171066,8159790  chr1      0 Rest_3,Rest_4,Rest_5,Rest_6            uc001aah.4   14361  29370
# 2:                         7896761  chr1      0             Rest_17,Rest_18 uc001abw.1,uc001abx.1  861120 879961
于 2013-01-23T13:07:29.457 に答える
1

を使用して 2 ステップで実行できますbystr_cin stringrpackageを使用して、1 つの文字列に連結します。タブはあなたのデータだと思います。

x1 <- by(tab,tab$ID2,FUN=function(x)       ## I group by ID2
{

  ID <- str_c(x$ID,collapse=',')
  probe <- str_c(x$probe,collapse=',')
  x <- x[1,]
  x$ID <- ID
  x$prob <- probe
  x
})
x1 <- do.call(rbind,x1)                   ## To change from a list to a data.frame

x2 <- by(x1,x1$probe,FUN=function(x)      ## I group by probe
{
  ID2 = str_c(x$ID2,collapse=',')
  txEnd = min(x$txEnd)
  txStart = max(x$txStart)
  x <- x[1,]
  x$ID2 <- ID2
  x$txEnd <- txEnd
  x$txStart <- txStart 
  x
})

x2 <- do.call(rbind,x2)     ## To change from a list to a data.frame

x2
                                 ID                   ID2   probe chrom strand txStart  txEnd                            prob
7896761                     Rest_17 uc001abw.1,uc001abx.1 7896761  chr1      0  871151 879961                         7896761
8044649 Rest_3,Rest_4,Rest_5,Rest_6            uc001aah.4 8044649  chr1      0   14361  29370 8044649,7911309,8171066,8159790
于 2013-01-23T11:05:32.003 に答える