有没有一个R函数可以找到一行中最高的值,而不与列中的其他名称匹配?

huangapple go评论63阅读模式
英文:

Is there an R function to find the highest value in a row that does not match other names in a column?

问题

以下是我目前正在使用的R代码:

library('tidyverse')
library('dplyr')

Brian <- c(92.835, 89.035, 99.222, 93.581)
Buckley <- c(75.265, 86.258, 93.972, 96.872)
Chris <- c(91.442, 103.999, 91.291, 92.505)
Catherine <- c(81.244, 73.040, 78.455, 98.972)
David <- c(87.153, 60.062, 62.248, 87.852)
Donald <- c(93.395, 91.905, 102.502, 107.63)
Greg <- c(79.571, 73.702, 67.326, 89.493)
Matt <- c(78.585, 48.074, 81.387, 76.074)
Michael <- c(96.933, 78.709, 82.623, 66.325)

df <- data.frame(Brian, Buckley, Chris, Catherine, David, Donald, Greg, Matt, Michael)
group1 <- data.frame(Brian, Matt, Michael)
group2 <- data.frame(Buckley, Chris, Catherine)
group3 <- data.frame(David, Donald, Greg)

group1a <- group1 %>%
  mutate(Group1 = names(.)[max.col(.)])

group2a <- group2 %>%
  mutate(Group2 = names(.)[max.col(.)])

group3a <- group3 %>%
  mutate(Group3 = names(.)[max.col(.)])

GROUP1 <- dplyr::pull(group1a, 'Group1')
GROUP2 <- dplyr::pull(group2a, 'Group2')
GROUP3 <- dplyr::pull(group3a, 'Group3')

ALL <- cbind(df, GROUP1, GROUP2, GROUP3)

该代码显示了一个较长表格的前4行。我一直在努力寻找一段代码,可以将一个列附加到这个表格中,该列包括不是列GROUP1、GROUP2、GROUP3中的列中具有最高值的列的名称。输出列将以“GROUP4”为标题,并包括这四行的Brian、Buckley、Chris、Buckley。

我尝试在dplyr中查找适合这个问题的代码,但我是新手,一直在困惑。

英文:

有没有一个R函数可以找到一行中最高的值,而不与列中的其他名称匹配?

Here is the R code I am currently using:

library(&#39;tidyverse&#39;)
library (&#39;dplyr&#39;)

Brian &lt;- c(92.835, 89.035, 99.222, 93.581)
Buckley &lt;- c(75.265, 86.258, 93.972, 96.872)
Chris &lt;- c(91.442, 103.999, 91.291, 92.505)
Catherine &lt;- c(81.244, 73.040, 78.455, 98.972)
David &lt;- c(87.153, 60.062, 62.248, 87.852)
Donald &lt;- c(93.395, 91.905, 102.502, 107.63)
Greg &lt;- c(79.571, 73.702, 67.326, 89.493)
Matt &lt;- c(78.585, 48.074, 81.387, 76.074)
Michael &lt;- c(96.933, 78.709, 82.623, 66.325)

df &lt;- data.frame(Brian, Buckley, Chris, Catherine, David, Donald, Greg, Matt, Michael)
group1 &lt;- data.frame(Brian, Matt, Michael)
group2 &lt;- data.frame(Buckley, Chris, Catherine)
group3 &lt;- data.frame(David, Donald, Greg)

group1a &lt;- group1 %&gt;%
  mutate(Group1 = names(.)[max.col(.)])

group2a &lt;- group2 %&gt;%
  mutate(Group2 = names(.)[max.col(.)])

group3a &lt;- group3 %&gt;%
  mutate(Group3 = names(.)[max.col(.)])

GROUP1 &lt;- dplyr::pull(group1a, &#39;Group1&#39;)
GROUP2 &lt;- dplyr::pull(group2a, &#39;Group2&#39;)
GROUP3 &lt;- dplyr::pull(group3a, &#39;Group3&#39;)

ALL &lt;- cbind(df, GROUP1, GROUP2, GROUP3)

The code shows 4 rows of a much longer table. I've been stumped trying to find code that will allow me to append a column to this table that includes the name of the column from the highest value in the row that is not one of the names in the columns GROUP1, GROUP2, GROUP3. The output column would be headed "GROUP4", and would include Brian, Buckley, Chris, Buckley for these four rows.

I've tried looking through dplyr for codes that would fit this problem, but I'm new to this and have been stumped for a bit.

答案1

得分: 0

(possible_names <- select(
  ALL,
  where(is.numeric)
) |> names())
(excl_names <- select(
  ALL,
  where(\(x)!is.numeric(x))
) |> names())

ALL2 <- mutate(rowwise(ALL),
  cols_to_check = list(setdiff(
    possible_names,
    c_across(all_of(excl_names))
  ))
)

(ALL3 <- ALL2 |> mutate(GROUP4 = (\(x){
  x[max.col(pick(x))]})(c_across(cols_to_check))
  ) |> ungroup() |> select(-cols_to_check))
英文:

(possible_names &lt;- select(
  ALL,
  where(is.numeric)
) |&gt; names())
(excl_names &lt;- select(
  ALL,
  where(\(x)!is.numeric(x))
) |&gt; names())

ALL2 &lt;- mutate(rowwise(ALL),
  cols_to_check = list(setdiff(
    possible_names,
    c_across(all_of(excl_names))
  ))
)

(ALL3 &lt;- ALL2 |&gt; mutate(GROUP4 = (\(x){
  x[max.col(pick(x))]})(c_across(cols_to_check))
  ) |&gt; ungroup() |&gt; select(-cols_to_check))


</details>



huangapple
  • 本文由 发表于 2023年6月22日 03:52:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76526700.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定