R 在不使用汇总或过滤器的情况下查找观测子集中的最大值
大家,
我有以下数据框示例,其中包含患者的 ID(1 和 2)、其类别 X(是或否)以及参数的值
df <- data.frame (ID = c (1, 1, 1, 1, 2, 2, 2),
X = c ("YES", "YES", "NO", "NO", "YES", "NO", "NO"),
Value = c (10, 15, 12, 13, 18, 16, 17))
df
这提供了下表:
ID X Value
1 1 YES 10
2 1 YES 15
3 1 NO 12
4 1 NO 13
5 2 YES 18
6 2 NO 16
7 2 NO 17
我想获得新的列结果这将给出每个患者的最大值,对 X 列中的“是”做出如下响应
ID X Value Result
1 1 YES 10 15
2 1 YES 15 15
3 1 NO 12 15
4 1 NO 13 15
5 2 YES 18 18
6 2 NO 16 18
7 2 NO 19 18
我知道我可以使用 group_by 并汇总来获取值,但我想使用 mutate 以便我可以跟踪所有变量我为这个项目构建的原因,避免过滤功能。
以下解决方案为我提供了结果列,但我再次希望每个 ID 仅有一个值。
df %>%
group_by(ID,X)%>%
mutate (Result = max(Value))
ID X Value Result
<dbl> <chr> <dbl> <dbl>
1 1 YES 10 15
2 1 YES 15 15
3 1 NO 12 13
4 1 NO 13 13
5 2 YES 18 18
6 2 NO 16 19
7 2 NO 19 19
非常感谢您的帮助
everyone,
I have the following dataframe example, with the ID of patients (1 and 2), their category X (YES or NO), and the values of a parameter
df <- data.frame (ID = c (1, 1, 1, 1, 2, 2, 2),
X = c ("YES", "YES", "NO", "NO", "YES", "NO", "NO"),
Value = c (10, 15, 12, 13, 18, 16, 17))
df
This provides the following table:
ID X Value
1 1 YES 10
2 1 YES 15
3 1 NO 12
4 1 NO 13
5 2 YES 18
6 2 NO 16
7 2 NO 17
I would like to get a new column result that would give the maximum value, per patient, responding to "YES" in the column X as follows
ID X Value Result
1 1 YES 10 15
2 1 YES 15 15
3 1 NO 12 15
4 1 NO 13 15
5 2 YES 18 18
6 2 NO 16 18
7 2 NO 19 18
I know that I can use group_by and summarise to obtain the values, but I would like to use mutate so that I can follow all the variables that I build for this project, and for the same reason, avoid the filter function.
The following solution provides me the result column, but again I would like only one value per ID.
df %>%
group_by(ID,X)%>%
mutate (Result = max(Value))
ID X Value Result
<dbl> <chr> <dbl> <dbl>
1 1 YES 10 15
2 1 YES 15 15
3 1 NO 12 13
4 1 NO 13 13
5 2 YES 18 18
6 2 NO 16 19
7 2 NO 19 19
Thank you very much for your help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用此代码:
You can use this code:
使用
data.table
Using
data.table
这又如何呢?
What about this?