如何检索R data.table中按行最大值的列?

编程入门 行业动态 更新时间:2024-10-22 17:27:21
本文介绍了如何检索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中按行最大值的列?

本文发布于:2023-10-27 03:05:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1532126.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:最大值   data   table   中按行

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!