Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 重复条目池,同时平均相邻列中的值_R_Dataframe - Fatal编程技术网

R 重复条目池,同时平均相邻列中的值

R 重复条目池,同时平均相邻列中的值,r,dataframe,R,Dataframe,我正在尝试做一些复杂的索引,同时进行平均、合并以及取最小值和最大值。这是一个示例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

我正在尝试做一些复杂的索引,同时进行平均、合并以及取最小值和最大值。这是一个示例
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
我使用
duplicated
在ID2列中查找重复项:uc001aah.4有4个重复项。但是,我需要并且不知道如何做的是,uc001aah.4只有一个条目,然后将探测列(+一些其他)条目合并到一个单元格中(以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
因此,在这里,我需要将ID和ID2合并,同时获取列txStart的最小值和列txEnd的最大值,最终得到:

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

我知道这要求很高,但如果你在第一个问题上告诉我如何做,我相信我将能够找出如何将其应用于第二个问题。

你可以使用
by
分两步完成。我在
stringr
包中使用
str\u c
将字符串连接在一起。我假设tab是您的数据

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

x1使用
数据的解决方案。表

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
require(data.table)
dt
#ID ID2探针铬绞线txStart txEnd
#1:剩余uc001aah.4 8044649 chr1 0 14361 29370
#2:其余4 uc001aah.4 7911309 chr1 0 14361 29370
#3:剩余5 uc001aah.4 8171066 chr1 0 14361 29370
#4:剩余6 uc001aah.4 8159790 chr1 0 14361 29370
#5:剩余部分17 uc001abw.1 7896761 chr1 0 861120 879961
#6:Rest_18 uc001abx.1 7896761 chr1 0 871151 879961
#步骤1:删除重复的ID2并连接ID和探测器。
#注意:这里我假设如果ID2相同,那么chrom也一样,
#绞线、txStart和txEnd。如果没有,则可以修改此类似项
#到步骤2中的内容。
dt.out