首页 文章

找到范围内的最大值,然后返回该行的列名称

提问于
浏览
1

我有data.frame,列名以X前缀和一系列数字开头 . 例如,

col<-c("X1.1","X1.2","X1.3","X1.4","X1.5","X2.1","X2.2","X2.3","X2.4","X2.5","X3.1","X3.2","X3.3","X3.4","X3.5")
m<-matrix(sample(1:15),ncol=15,nrow=5)
mf<-data.frame(m)
colnames(mf)<-col

然后我想找到X1前缀(总共四列),X2(四列),X3(四列)中每行的最大值...并返回列号(X前缀后面的后续数字)最大值

所以我的预期输出是


X1  X2  X3  X4
1    4   2   4  ...
...

谁可以帮我这个事?如果有两个最大值,那么也想要返回两个列名...

我搜索了 which 应该被使用..但不确定 .

2 回答

  • 2

    重新创建示例数据(请在将来使用 reproducedput ):

    df = data.frame(matrix(rep(NA,12*3),nrow=3))
    colnames(df) = strsplit("X1.1 X1.2 X.3 X.4 X2.1 X2.2 X2.3 X2.4 X3.1 X3.2 X3.3 X3.4",split=" ")[[1]]
    sapply(colnames(df), function(x) { df[[x]] <<- sample(1:10,3) } )
    

    获取不同类型的colnames:

    xTypes = unique(sapply(colnames(df), function(x) { strsplit(x,"\\.")[[1]][1] } ))
    

    获取每个colname类型的最大值:

    result = sapply(xTypes,function(x) { max(df[,grep(paste(x,"\\.",sep=""),colnames(df))])  })
    
    > sapply(xTypes,function(x) { max(df[,grep(paste(x,"\\.",sep=""),colnames(df))])  })
    X1  X X2 X3 
     9  9 10  9
    

    如果您希望每个colname类型中的列索引最多:

    result = sapply(xTypes,function(x) { which.max(apply(df[,grep(paste(x,"\\.",sep=""),colnames(df))],2,max))  })
    names(result) = xTypes
    

    结果是:

    X1  X X2 X3 
     1  1  2  1
    
  • 3

    要重塑您的数据,请使用以下内容:

    library(reshape2)
    mf.melted <- melt(data=mf)
    mf.melted$group <- unlist(gsub("\\.\\d+$", "", as.character(mf.melted$variable)))
    mf.melted
    

    此行的删除:unlist(gsub(“\ . \ d $”,“”,as.character(mf.melted $ variable)))

    ## Original column names are now stored as column `'variable'` in `mf.melted`
    mf.melted$variable
    
    ## Notice it is a `factor` column. So needs to be converted to string. This is done with:
    as.character(  __  )
    
    ## Next we remove the `.3` (or whatever number) from each.
    ## the regex expression '\\.\\d+$' looks for 
    `\\.`  # a period
    `\\d`  # a digit
    '\\d+' # at least one digit
    `$`    # at the end of a word
    
    ## gsub  finds the first pattern and replaces it with the second
    ## in this case an empty string
    gsub("\\.\\d+$", "",  __ )
    
    ## We then assign the results back into a new column, namely `'group'`
    mf.melted$group <-   __
    

    现在,通过熔化的data.frame,您可以轻松地按列组进行搜索和聚合

    head(mf.melted)
      variable value group
    1     X1.1     3    X1
    2     X1.1     4    X1
    3     X1.1    12    X1
    4     X1.1    14    X1
    5     X1.1     7    X1
    6     X1.2     6    X1
    

相关问题