简体   繁体   English

R 使用 dplyr 根据列中的最大值变异 dataframe

[英]R mutate a dataframe based on max in a column using dplyr

I want to use ddply or group_by to mutate an existing dataframe based on the values in one of the columns in the dataframe.我想使用 ddply 或 group_by 根据 dataframe 中的一列中的值来改变现有的 dataframe。

I have a dataframe with 3 columns.我有一个 3 列的 dataframe。 I want to identify the ROI within each ID and Condition that has the maximum value in df$Value.我想确定在 df$Value 中具有最大值每个 ID 和 Condition 中的 ROI。 So for the following df, ROI 3 would be called Max for ID 1+Match condition, ROI 4 would be Max for ID 1+NoMatch Condition and so on.因此对于以下 df,对于 ID 1+Match 条件,ROI 3 将被称为 Max,对于 ID 1+NoMatch 条件,ROI 4 将被称为 Max,依此类推。

set.seed(1)
df <- data.frame("ID"=sort(rep_len(1:2, 12)), "ROI"=rep_len(1:6, 12), "Condition"=rep_len(c(rep_len("Match", 3), rep_len("NoMatch", 3)), 12), "Value"=runif(12), MaxROI="None")

I tried using some combinations of ddply and group_by.我尝试使用 ddply 和 group_by 的一些组合。 For instance:例如:

ddply(df, c("ID", "Condition"), mutate, MaxROI[which.max(Value)]="Max")

#generates an error
#Error: unexpected '=' in "ddply(df, c("ID", "Condition"), mutate, MaxROI[which.max(Value)]="

I have looked here , but I don't want to filter the dataframe to keep the rows with max values, but mutate the existing df.我看过这里,但我不想过滤 dataframe 以保留具有最大值的行,而是改变现有的df。

Thank you,谢谢,

Mrinmayi姆林马伊

We can use dplyr .我们可以使用dplyr After grouping by 'ID', 'Condition', create the column 'Max' by comparing the 'Value' with max of 'Value' in case_when to create the "Max" string where there is a max 'Value' or else by default it is NA按“ID”、“条件”分组后,通过将“值”与“值”的max进行比较来创建“最大值”列,在case_when中创建“最大值”字符串,其中存在最大值“值”或默认情况下它是不适用

library(dplyr)
df %>% 
   group_by(ID, Condition) %>% 
   dplyr::mutate(Max =case_when(Value == max(Value) ~ "Max"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM