I次のR data.tableを持っている: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
私は次のコードを使用します。
を出力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
、私は実際値に興味がないんだけど、値はから来た列、すなわち、私は次の出力をしたいと思います:
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
です)。
列名を最大値で「取得」するにはどうすればよいですか?ここで
[this](https://stackoverflow.com/questions/28909684/calculate-row-wise-maximum/28910623#28910623)または[this](https://stackoverflow.com/ques)の可能な複製33769378#33769378) –