如何检索R data.table中按行最大值的列? [英] How to retrieve column for row-wise maximum value in an R data.table?
问题描述
我有以下R data.table:
I have the following R data.table:
library(data.table)
iris = as.data.table(iris)
> iris
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa
7 4.6 3.4 1.4 0.3 setosa
8 5.0 3.4 1.5 0.2 setosa
...
假设我只想为data.table列的子集按行查找逐行最大值:Sepal.Length
,Sepal.Width
,Petal.Length
,Petal.Width
Let's say I wanted to find the row-wise maximum value by each row, only for the subset of data.table columns: Sepal.Length
, Sepal.Width
, Petal.Length
, Petal.Width
我将使用以下代码:
iris[, maximum_element :=max(Sepal.Length, Sepal.Width, Petal.Length, Petal.Width), by=1:nrow(iris)]
哪个输出
Sepal.Length Sepal.Width Petal.Length Petal.Width Species maximum_element
1: 5.1 3.5 1.4 0.2 setosa 5.1
2: 4.9 3.0 1.4 0.2 setosa 4.9
3: 4.7 3.2 1.3 0.2 setosa 4.7
4: 4.6 3.1 1.5 0.2 setosa 4.6
5: 5.0 3.6 1.4 0.2 setosa 5.0
对于我的问题,我实际上对值不感兴趣,但是该值来自哪一列,即我想要以下输出:
For my problem, I'm actually not interested in the value, but which column the value came from, i.e. I would like the following output:
Sepal.Length Sepal.Width Petal.Length Petal.Width Species maximum_column
1: 5.1 3.5 1.4 0.2 setosa Sepal.Length
2: 4.9 3.0 1.4 0.2 setosa Sepal.Length
3: 4.7 3.2 1.3 0.2 setosa Sepal.Length
4: 4.6 3.1 1.5 0.2 setosa Sepal.Length
5: 5.0 3.6 1.4 0.2 setosa Sepal.Length
(在这种情况下,最大值分别来自Sepal.Length
).
(In this case, the max. value each comes from Sepal.Length
).
如何检索"具有最大值的列名?
How do I "retrieve" the column name with the maximum value?
推荐答案
以下是pmax
iris[, maximum_element := do.call(pmax, .SD), .SDcols = 1:4]
并找到列名,在将.SDcols
指定为数字列(即第1到第4列)后,在.SD
上使用max.col
and to find the column names, use max.col
on .SD
after specifying the .SDcols
as the numeric columns, i.e. columns 1 to 4
iris[,maximum_column := names(.SD)[max.col(.SD)], .SDcols = 1:4]
head(iris, 4)
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species maximum_column
#1: 5.1 3.5 1.4 0.2 setosa Sepal.Length
#2: 4.9 3.0 1.4 0.2 setosa Sepal.Length
#3: 4.7 3.2 1.3 0.2 setosa Sepal.Length
#4: 4.6 3.1 1.5 0.2 setosa Sepal.Length
这篇关于如何检索R data.table中按行最大值的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!