Getting column name which holds a max value within a row of a matrix holding a separate max value within an array

后端 未结 3 1914
遥遥无期
遥遥无期 2021-02-13 19:04

For instance given:

dim1 <- c("P","PO","C","T")
dim2 <- c("LL","RR","R","Y")         


        
3条回答
  •  我在风中等你
    2021-02-13 19:43

    Here's a simple way to solve:

      mxCol=function(df, colIni, colFim){ #201609
      if(missing(colIni)) colIni=1
      if(missing(colFim)) colFim=ncol(df)
      if(colIni>=colFim) { print('colIni>=ColFim'); return(NULL)}
      dfm=cbind(mxC=apply(df[colIni:colFim], 1, function(x) colnames(df)[which.max(x)+(colIni-1)])
               ,df)
      dfm=cbind(mxVal=as.numeric(apply(dfm,1,function(x) x[x[1]]))
               ,dfm)
      returndfm
    }
    

提交回复
热议问题