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

时间:2014-01-29 03:09:33

标签: r indexing which

我有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 个答案:

答案 0 :(得分:3)

重新创建示例数据(请在将来使用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) } )

获取不同种类的姓氏:

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

获取每种名字的最大种类:

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 

答案 1 :(得分:2)

要重塑您的数据,请使用以下命令:

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